C中sizeof(struct structname)和sizeof(object)的区别
在某些情况下,sizeof(struct Structure_name)
和 sizeof(object)
之间可能存在差异,其中 object
的类型为 struct C 中的结构名
?
Are there scenarios where there can be a difference between sizeof(struct structure_name)
and sizeof(object)
where object
is of type struct structure_name
in C?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,
sizeof(type)
和sizeof(o)
之间没有区别,其中o
的声明类型是类型
。如果对象的声明类型不能真正代表该对象,则可能会存在差异。例如,
出现这种差异是因为
sizeof
是 C 中的编译时构造。因此,没有运行时分析,编译器会查看静态声明的类型。No there is no difference between
sizeof(type)
andsizeof(o)
where the declared type ofo
istype
.There can be differences if the declared type of the object isn't truly representative of the object. For example
This difference occurs because
sizeof
is a compile time construct in C. Hence there is no runtime analysis and the compiler looks instead at the statically declared types.不;
sizeof(type)
和sizeof(object-of-type)
始终产生相同的结果。嗯,需要注意的是,对于 VLA(可变长度数组),sizeof() 可能是运行时操作,但除此之外,它们是固定的。您可以证明,即使使用 VLA,
sizeof(variously-qualified-type)
和sizeof(object-of-same-variously-qualified-type)
的结果也是如此code> 将产生相同的结果,因此 VLA 并不是真正的例外。No;
sizeof(type)
andsizeof(object-of-type)
produce the same result at all times.Well, there's a caveat that with a VLA (variable-length array), the
sizeof()
might be a run-time operation, but otherwise, they are fixed. And you can make a case that even with a VLA, the result ofsizeof(variably-qualified-type)
andsizeof(object-of-same-variably-qualified-type)
will produce the same result, so VLAs are not really an exception.