可以的sizeof返回0(零) [英] Can sizeof return 0 (zero)

查看:854
本文介绍了可以的sizeof返回0(零)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能为sizeof操作符来曾经在C或C ++返回0(零)?如果可能的话,它是从一个标准的角度是否正确?

Is it possible for the sizeof operator to ever return 0 (zero) in C or C++? If it is possible, is it correct from a standards point of view?

推荐答案

在C ++中的空类或结构的定义有一个的sizeof 至少为1。从C ++标准,9/3类:完整对象和类类型的成员子对象应具有非零大小。

In C++ an empty class or struct has a sizeof at least 1 by definition. From the C++ standard, 9/3 "Classes": "Complete objects and member subobjects of class type shall have nonzero size."

在C空的结构是不允许的,除非通过扩展(或编译器的一个漏洞)。

In C an empty struct is not permitted, except by extension (or a flaw in the compiler).

这是语法(这要求有括号里面的东西)从6.7.2.1/7结构和联合说明这句话沿的后果:如果结构声明列表中没有名称的成员,其行为是未定义的。

This is a consequence of the grammar (which requires that there be something inside the braces) along with this sentence from 6.7.2.1/7 "Structure and union specifiers": "If the struct-declaration-list contains no named members, the behavior is undefined".

如果一个零大小的结构是允许的,那么它是一个语言扩展(或在编译器的一个缺陷)。例如,在GCC扩展在<一个记录href=\"http://gcc.gnu.org/onlinedocs/gcc-4.4.3/gcc/Empty-Structures.html#Empty-Structures\">\"Structures不带任何成员,它说:

If a zero-sized structure is permitted, then it's a language extension (or a flaw in the compiler). For example, in GCC the extension is documented in "Structures with No Members", which says:

GCC允许C结构有没有成员:

GCC permits a C structure to have no members:

 struct empty {
 };


  
  

的结构将有大小为零。在C ++中,空结构是语言的一部分。 G ++把空的结构,因为如果他们有类型的单个成员字符

这篇关于可以的sizeof返回0(零)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆