当铸造一个int数组短*,为什么赋值给一个元素覆盖了整个整数? [英] When casting an int array to a short*, why does assigning a value to an element overwrite the entire integer?
问题描述
I am watching Jerry Cain's Programming Paradigms Lecture 3 video where the effect of an element assignment after casting between an int array and short array is demonstrated. Essentially the argument is that if you were to assign an int array element arr[3] = 128
, then temporarily cast the int array to a short* and assign arr[6] = 2
, then arr[3] should become 128 + 512 = 640 because the 2 would be interpreted as being in the 2^9th position. Code to demonstrate:
#include <stdio.h>
int main() {
printf("sizeof(int) is %lu\n", sizeof(int));
printf("sizeof(short) is %lu\n", sizeof(short));
int arr[5];
arr[3] = 128;
((short*)arr)[6] = 2;
printf("arr[3] is equal to %d\n", arr[3]); //expect 640, get 2 instead
return 0;
}
当我虽然运行此code,我得到以下的输出:
When I run this code though, I get the following output:
sizeof(int) is 4
sizeof(short) is 2
arr[3] is equal to 2
我期待常用3 [3]等于640,而是它根本就等于2。我承认一个C小白 - ?任何人都可以解释
I expect arr[3] to be equal to 640, but instead it is simply equal to 2. I am admittedly a C noob - can anyone explain?
推荐答案
大端VS little-endian的,我想。
Big-endian vs little-endian, I think.
在code本质上是平台特定的(正式,几乎可以肯定,不确定的行为)。我不知道你应该被教它,但这是,我想,其他时间的问题。
The code is inherently platform-specific (officially, almost certainly undefined behaviour). I'm not sure you should be being taught it, but that is, I guess, an issue for another time.
2.被分配到两个ARR四个字节的[3]
。如果您分配给((短*)ARR)[7]
相反,你可能会看到预期的结果。
The 2 is assigned to two of the four bytes of arr[3]
. If you assigned to ((short *)arr)[7]
instead, you might see the expected result.
什么机器,你的测试(CPU是什么类型的)?
What machine are you testing on (what type of CPU)?
在第二个想法 - 尽管这个问题的一部分,也许是大端VS小端,另一个问题是短
VS 字符
。这里的一些code,显示各种途径来解决:
On second thoughts - although part of the issue is perhaps big-endian vs little-endian, the other problem is short
vs char
. Here's some more code that shows various pathways to the solution:
#include <stdio.h>
int main(void)
{
printf("sizeof(int) is %lu\n", sizeof(int));
printf("sizeof(short) is %lu\n", sizeof(short));
int arr[5];
arr[3] = 128;
((short*)arr)[6] = 2;
printf("arr[3] is equal to %8d (0x%08X)\n", arr[3], arr[3]);
arr[3] = 128;
((short*)arr)[7] = 2;
printf("arr[3] is equal to %8d (0x%08X)\n", arr[3], arr[3]);
for (int i = 12; i < 16; i++)
{
arr[3] = 128;
((char *)arr)[i] = 2;
printf("arr[3] is equal to %8d (0x%08X) i = %d\n", arr[3], arr[3], i);
}
return 0;
}
这个修订code的输出是:
The output of this revised code is:
sizeof(int) is 4
sizeof(short) is 2
arr[3] is equal to 2 (0x00000002)
arr[3] is equal to 131200 (0x00020080)
arr[3] is equal to 2 (0x00000002) i = 12
arr[3] is equal to 640 (0x00000280) i = 13
arr[3] is equal to 131200 (0x00020080) i = 14
arr[3] is equal to 33554560 (0x02000080) i = 15
测试上的MacOS X 10.7.2使用GCC 4.2.1 X $ C $ 4.2Ç(LLVM)。
Testing on MacOS X 10.7.2 with GCC 4.2.1 XCode 4.2 (LLVM).
这篇关于当铸造一个int数组短*,为什么赋值给一个元素覆盖了整个整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!