給定的C代碼不相容C和C++之間代碼
#include <stdio.h>
int x = 14;
size_t check()
{
struct x {};
return sizeof(x); // which x
}
int main()
{
printf("%zu",check());
return 0;
}
給出4在C輸出對我的32位實施方案,而在C++代碼
#include <iostream>
int x = 14;
size_t check()
{
struct x {};
return sizeof(x); // which x
}
int main()
{
std::cout<< check();
return 0;
}
輸出1爲什麼這種差異?
可能的重複項:http://stackoverflow.com/questions/3451266/understanding-sizeofchar-in-32-bit-c-compilers http://stackoverflow.com/questions/119123/why-isnt-sizeof-for -a-struct-equal-of-sum-of-sizeof-of-each-member – phooji 2011-03-19 08:44:44
@phooji:不,這不是重複的。 – 2011-03-19 08:46:26
@Saurabh:你可能是對的 - 這些問題並不完全一樣,但我認爲答案是相關的(請參閱Prasoon的回答以及我對你的回答的評論)。 – phooji 2011-03-19 08:51:05