【发布时间】:2017-04-21 22:32:51
【问题描述】:
最近我开始学习 C/C++,目前正在研究低级编程。我发现“联合”非常有趣,因为我以前从未见过这样的行为,可以直接用我学过的任何语言共享记忆。现在我想知道是否可以通过使用 Union 之类的东西或通过 以某种方式 将整数指向数组的特定索引以获得与以下相同的结果来提高这个简单例程的性能.
// An Union struct which holds a 32bit integer where every of the 4 bytes can be accessed.
typedef union
{
struct
{
unsigned char BYTE1;
unsigned char BYTE2;
unsigned char BYTE3;
unsigned char BYTE4;
} BYTES;
unsigned long VALUE;
} UnionDWORD;
// An array of unsigned chars - representing memory
unsigned char memory[1024]; // reserving 1024 bytes of memory
// Here is where I'm wondering if the "putting together" of the 4 bytes can be improved. (Maybe a pointer to the array?)
// Return 4bytes from memory at the position "pos"
unsigned long getDWordFromMemory(unsigned long pos)
{
// Making use of the Union struct to put the 4 bytes together
UnionDWORD result;
result.BYTES.BYTE4 = memory[pos];
result.BYTES.BYTE3 = memory[pos+1];
result.BYTES.BYTE2 = memory[pos+2];
result.BYTES.BYTE1 = memory[pos+3];
return result.VALUE;
}
非常感谢,我真的很抱歉我的英语。不幸的是,这不是我的母语。
【问题讨论】:
-
我不确定,但也许你可以用
*int指向数组,然后编译器将读取 4 个字节作为数字而不是1。因此,如果您执行newPtr[0],则它的前 4 个字节为整数。 -
这已经非常高效了(假设是 C,而不是 C++,所以没有严格的别名问题)。我怀疑你能写出比这更好的东西;编译器应该能够识别正在发生的事情
-
C 或 C++,选择一个。它们不是同一种语言。
-
顺便说一句:
unsigned long可能不是 4unsigned char,即使这很常见。考虑uint32_t
标签: c arrays shared-memory unions