sizeof()什么时候返回0

ibingow 2012-04-12 02:07:52
在看webkit,看到JavaScriptCore/wtf/OwnPtrCommon.h有这么一段

template <typename T> inline void deleteOwnedPtr(T* ptr)
{
typedef char known[sizeof(T) ? 1 : -1];
if (sizeof(known))
delete ptr;
}


很奇怪,sizeof()还会返回0?什么时候会发生?
...全文
694 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
tinyhare 2013-02-04
  • 打赏
  • 举报
回复
我打了个代码sizeof返回零了,求解。 代码: long long int a=3; printf("%ld,%d\n",a,sizeof(a)); 编译警告: warning: format ‘%ld’ expects type ‘long int’, but argument 2 has type ‘long long int’ 运行结果: 3,0 如果将第一个%ld按警告改为%lld则无警告,运行结果就是3,8为什么呢? ubuntu gcc编译器, gcc --version gcc (Ubuntu/Linaro 4.4.4-14ubuntu5.1) 4.4.5
ibingow 2012-04-17
  • 打赏
  • 举报
回复
有些编译器支持,所以为了周全,webkit梨还是这么写了,应该这样

[Quote=引用 15 楼 的回复:]

In a declarationT D where D has the form
D1 [constant-expressionopt]
and the type of the identifier in the declarationT D1 is “derived-declarator-type-list T,” then the type of the
identifier of D……
[/Quote]
pengzhixi 2012-04-17
  • 打赏
  • 举报
回复
In a declarationT D where D has the form
D1 [constant-expressionopt]
and the type of the identifier in the declarationT D1 is “derived-declarator-type-list T,” then the type of the
identifier of D is an array type.

If the constant-expression
(5.19) is present, it shall be an integral constant expression and its value shall be greater than zero.

所以支持数组元素个数为0的是没有可移植性的
RabbitLBJ 2012-04-17
  • 打赏
  • 举报
回复
#define sizeof(X) 0
ibingow 2012-04-17
  • 打赏
  • 举报
回复
试了下下标为0果然可以,至少gcc编译通过。以前看到过一些静态诊断用的就是数组大小不能为0,看来也不正确。
[Quote=引用 5 楼 的回复:]

简单地说,没有,但是编译器都有扩展。其中有两个扩展。

int buf[0];
sizeof(buf); //0

另一个是
当sizeof 非法时,也就是说产生了编译错误,如同lz给出的代码。
试试这样
void * p = 0;
deleteOwnedPtr(p); //编译错误 void 不能用于sizeof
[/Quote]
Kaile 2012-04-14
  • 打赏
  • 举报
回复
可能是想编译期检查一些错误
sducn1 2012-04-14
  • 打赏
  • 举报
回复
哪位老大帮我顶一下我的帖子啊
http://topic.csdn.net/u/20120324/10/a128ed09-e604-4fb7-86f4-305c320566f0.html
logiciel 2012-04-14
  • 打赏
  • 举报
回复
http://stackoverflow.com/questions/2632021/can-sizeof-return-0-zero

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?

Answer:
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."

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

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".

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 permits a C structure to have no members:
struct empty {
};

The structure will have size zero. In C++, empty structures are part of the language. G++ treats empty structures as if they had a single member of type char.
vcf_reader 2012-04-14
  • 打赏
  • 举报
回复
出错时,可以
csnd_life 2012-04-12
  • 打赏
  • 举报
回复
真还不清楚,想学学
evencoming 2012-04-12
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]
简单地说,没有,但是编译器都有扩展。其中有两个扩展。

int buf[0];
sizeof(buf); //0

另一个是
当sizeof 非法时,也就是说产生了编译错误,如同lz给出的代码。
试试这样
void * p = 0;
deleteOwnedPtr(p); //编译错误 void 不能用于sizeof
[/Quote]
路过学习。
Coder_Y_Jao 2012-04-12
  • 打赏
  • 举报
回复
申明一个全局变量,
然后extern

比如:extern int a[];
sizeof(a)

这个有些编译器会给警告,有些编译器编译不过,,不过在删掉sizeof那句话后在调试中取看sizeof(a)的话都是0
Jinhao 2012-04-12
  • 打赏
  • 举报
回复
简单地说,没有,但是编译器都有扩展。其中有两个扩展。

int buf[0];
sizeof(buf); //0

另一个是
当sizeof 非法时,也就是说产生了编译错误,如同lz给出的代码。
试试这样
void * p = 0;
deleteOwnedPtr(p); //编译错误 void 不能用于sizeof
wjb_yd 2012-04-12
  • 打赏
  • 举报
回复
typedef char ch[0]; typedef char ch[-1] 在VS2008下均编译不通过。
风雪夜归人 2012-04-12
  • 打赏
  • 举报
回复
未知。。。
muyi66 2012-04-12
  • 打赏
  • 举报
回复
没见识过...
fishion 2012-04-12
  • 打赏
  • 举报
回复
。。还真不知道。。类都有1字节的占位的

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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