结构的大小 [英] size of a struct
问题描述
考虑以下程序片段:
/ ************************ /
struct some_struct {
} a;
printf("%d",sizeof(a));
/ ************************* /
在GCC 4.1上。 1输出为0(零)。
在另一个编译器上{抱歉我不知道编译器:-(}输出
是1(一)。
是否依赖于编译器?
如果是这样的话,像a这样的变量怎么可能有一个零大小(我的意思是怎么可以一个
变量,0(零)字节大小存储在内存中。
chandanlinster写道:
考虑以下程序片段:
/ ******************* ***** /
struct some_struct {
} a;
printf("%d" ;,sizeof(a));
/ ************************* /
>
在GCC 4.1.1上的outpu t是0(零)。
在另一个编译器上{抱歉我不知道编译器:-(}输出
是1(一)。 />
是否依赖于编译器?
如果是这样,变量如何像a一样?零大小(我的意思是如何将具有0(零)字节大小的
变量存储在内存中)。
首先sizeof返回unsigned int所以它应该是
printf("%u",sizeof(a));
你所写的东西唤起了未定义的行为。
其次你声明一个没有成员的结构
这意味着你不能分配任何东西它。因此,零b / b $ b $看起来并不合理。
第三,我不知道没有会员的结构是否是
$标准允许b $ b。 GNU编译器发出
警告,而SUN编译器和lint给出语法
错误。所有这些都发生在以下代码中:
#include< stdio.h>
int main(void){
struct some_struct {} a;
printf("%u",sizeof(a));
}
" chandanlinster" < ch ************ @ gmail.comwrites:
请考虑以下程序片段:
/ ************************ /
struct some_struct {
} a;
printf("%d",sizeof(a));
/ ********* **************** /
标准C不允许没有成员的结构。
如果gcc允许这个,它是特定于编译器的扩展。如果您对它的工作原理感到好奇,请查看gcc文档;如果那个
失败了,试试gnu.gcc.help新闻组。
-
Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>
圣地亚哥超级计算机中心< *< http://users.sdsc.edu/~kst>
我们必须做点什么。这是事情。因此,我们必须这样做。
Spiros Bousbouras写道:
chandanlinster写道:
< blockquote class =post_quotes>
>考虑以下程序片段:
/ ********************** ** /
struct some_struct {
} a;
printf("%d",sizeof(a));
/ ** *********************** /
在GCC 4.1.1上,输出为0(零)。
开另一个编译器{抱歉我不知道编译器:-(}输出
是1(一)。
它是编译器依赖的吗?
如果是这样的话怎么可能像a这样的变量的大小为零(我的意思是如何将具有0(零)字节大小的变量存储在内存中)。
首先sizeof返回unsigned int所以它应该是
printf("%u",sizeof(a));
你写的东西唤起了未定义的行为。
它产生一个size_t,可能是也可能不是
一个unsigned int。
printf("%lu",(unsigned long)sizeof(a));通常建议用于
c89
Consider the following program fragment:
/************************/
struct some_struct {
}a;
printf("%d", sizeof(a));
/*************************/
On GCC 4.1.1 the output is 0(zero).
On another compiler {sorry I don''t know the compiler :-( } the output
is 1(one).
Is it compiler dependent?
If so how can a variable like "a" have a zero size ( I mean how can a
variable with 0(zero) byte size be stored in memory).
chandanlinster wrote:
Consider the following program fragment:
/************************/
struct some_struct {
}a;
printf("%d", sizeof(a));
/*************************/
On GCC 4.1.1 the output is 0(zero).
On another compiler {sorry I don''t know the compiler :-( } the output
is 1(one).
Is it compiler dependent?
If so how can a variable like "a" have a zero size ( I mean how can a
variable with 0(zero) byte size be stored in memory).First of all sizeof returns unsigned int so it should be
printf("%u", sizeof(a));
What you wrote evokes undefined behaviour.
Second you are declaring a structure with no members
which means you cannot assign anything to it. So a zero
size doesn''t look unreasonable.
Third , I don''t know if structures with no members are
allowed by the standard. The GNU compiler gives a
warning while the SUN compiler and lint give syntax
error. All this happens for the following code:
#include <stdio.h>
int main(void) {
struct some_struct { }a;
printf("%u", sizeof(a));
}
"chandanlinster" <ch************@gmail.comwrites:Consider the following program fragment:
/************************/
struct some_struct {
}a;
printf("%d", sizeof(a));
/*************************/Standard C doesn''t allow structures with no members.
If gcc allows this, it''s a compiler-specific extension. If you''re
curious about how it works, check the gcc documentation; if that
fails, try the gnu.gcc.help newsgroup.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Spiros Bousbouras wrote:chandanlinster wrote:
>Consider the following program fragment:
/************************/
struct some_struct {
}a;
printf("%d", sizeof(a));
/*************************/
On GCC 4.1.1 the output is 0(zero).
On another compiler {sorry I don''t know the compiler :-( } the output
is 1(one).
Is it compiler dependent?
If so how can a variable like "a" have a zero size ( I mean how can a
variable with 0(zero) byte size be stored in memory).
First of all sizeof returns unsigned int so it should be
printf("%u", sizeof(a));
What you wrote evokes undefined behaviour.It yields a size_t, which might or might not be
an unsigned int.
printf("%lu", (unsigned long)sizeof(a)); is usually recommended for
c89
这篇关于结构的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!