一个空结构体占用空间为什么是1?

jiajihe 2008-03-07 08:43:58
比如:

typedef struct test{

}TEST;

然后sizeof(TEST)的结果为1,这是为什么呢?

再如:
typdefr struct test_2{

void disp(void)
{
printf("testing\n");
}
}TEST_2;

sizeof(TEST_2)的结果依然是1;为什么?

...全文
470 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
sinosinux 2008-03-07
  • 打赏
  • 举报
回复
编译器需要标识它的存在
struct a
{};

a b[2];

如果 sizeof(a) = 0那么
&b[0] = &b[1], 所以,编译器需要一个占位字节
IlikeEnglish 2008-03-07
  • 打赏
  • 举报
回复
C世界里,都是0,不是1,c++就不同
yuanchuang 2008-03-07
  • 打赏
  • 举报
回复
方便寻址
molonvely 2008-03-07
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 molonvely 的回复:]
7.20.3 Memory management functions
1 The order and contiguity of storage allocated by successive calls to the calloc,
malloc, and realloc functions is unspecified. The pointer returned if the allocation
succeeds is suitably aligned so that it may be assigned to a pointer to any type of object
and then used to access such an object or an array of such objects in the space allocated
(unt…
[/Quote]
sizeof函数需要和内存分配函数一致
So...
molonvely 2008-03-07
  • 打赏
  • 举报
回复
7.20.3 Memory management functions
1 The order and contiguity of storage allocated by successive calls to the calloc,
malloc, and realloc functions is unspecified. The pointer returned if the allocation
succeeds is suitably aligned so that it may be assigned to a pointer to any type of object
and then used to access such an object or an array of such objects in the space allocated
(until the space is explicitly deallocated). The lifetime of an allocated object extends
from the allocation until the deallocation. Each such allocation shall yield a pointer to an
object disjoint from any other object. The pointer returned points to the start (lowest byte
address) of the allocated space. If the space cannot be allocated, a null pointer is
returned. If the size of the space requested is zero, the behavior is implementationdefined:
either a null pointer is returned, [color=#FFFF00]or the behavior is as if the size were some
nonzero value
, except that the returned pointer shall not be used to access an object.[/color]
大部分的库里malloc函数处理size=0时分配1个大小的空间。
好像只有GLIB的处理是返回一个NULL指针
晨星 2008-03-07
  • 打赏
  • 举报
回复
至少这样编译器处理起来容易一些,不至于导致两个不同的结构体变量的拥有相同的地址。
星羽 2008-03-07
  • 打赏
  • 举报
回复
即使是空的struct一样有大小的,不过是一个字节,
因为一个空结构,虽然没有内部成员,但是必须在内存上有所体现,不然在取地址的时候就出问题了,class也是一样 的

jixingzhong 2008-03-07
  • 打赏
  • 举报
回复
为了不使得 struct/class 实例堆叠在一起,
即使是没有任何成员,它们也会被填充 一个字节,
用于分隔各个对象实例
  • 打赏
  • 举报
回复
sizeof(TEST_2)的结果依然是1;为什么?
sizeof不计算函数
==================
这个问题建议参考一下
深度探索c++对象模型
qiucp 2008-03-07
  • 打赏
  • 举报
回复
试想一个“不
占空间”的变量如何被取地址、两个不同的“空结构体”变量又如何得以区分呢于是
,“空结构体”变量也得被存储,这样编译器也就只能为其分配一个字节的空间用于占
位了。如下:
struct S { };
sizeof( S ); // 结果为1
wolfdog1111 2008-03-07
  • 打赏
  • 举报
回复
是不是里面也包含默认对齐的原则?
laolaoliu2002 2008-03-07
  • 打赏
  • 举报
回复
sizeof(TEST_2)的结果依然是1;为什么?
sizeof不计算函数
  • 打赏
  • 举报
回复
主要为了区别两个实体,如果结果为没有地址,那比较指向那两个实体的指针就没地方指了,也没法比较两个不同指针的不同。
  • 打赏
  • 举报
回复
因为结构体类型变量需要在内存中存在,也就是说需要有地址,为0的时候你如何分配空间呢?
Supper_Jerry 2008-03-07
  • 打赏
  • 举报
回复
看看深度探索c++对象模型吧。
第3章有介绍的。
这个隐晦的1是编译器安插进去的一个char,使得2个空的struct或者class在内存中具有独一无二的地址
Treazy 2008-03-07
  • 打赏
  • 举报
回复
...The sizeof operator never yields 0, even for an empty class...

看看我以前写的文章,也许对你有帮助

http://blog.csdn.net/Treazy/archive/2007/12/21/1957287.aspx

69,379

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧