sizeof(struct name_of_struct) vs sizeof(name_of_struct) 之间的区
Difference between sizeof(struct name_of_struct) vs sizeof(name_of_struct)?
两者
之间有什么区别:
sizeof(struct name_of_struct)
与
sizeof(name_of_struct)
主题的返回值相同。
两者之间有什么区别吗?即使它很微妙/不重要,我也想知道。
struct name_of_struct
明确地指的是struct
/class
标记name_of_struct
而name_of_struct
可以是变量或函数名称。
例如,在 POSIX 中,您同时拥有struct stat
和一个名为 stat
的函数。当你想引用结构类型时,你需要 struct
关键字来消除歧义(+纯 C 总是需要它 - 在纯 C 中,常规标识符与结构标签位于单独的命名空间中,并且结构标签不会像它们在 C++ 中那样泄漏到常规标识符命名空间中,除非您像 typedef struct tag{ /*...*/ } tag;
中那样使用 typedef 将它们显式拖到那里(。
例:
struct foo{ char x [256];};
void (*foo)(void);
int structsz(){ return sizeof(struct foo); } //returns 256
int ptrsz(){ return sizeof(foo); } //returns typically 8 or 4
如果这看起来令人困惑,它基本上是为了保持与 C 的向后兼容性而存在的。
当然,
sizeof name_of_struct
仅当name_of_struct是对象(而不仅仅是类型(时,才为有效表达式;写入
sizeof(struct name_of_struct)
除非name_of_struct已经定义为(完整类型(为结构(或类(,否则不会编译。 但是等等,还有更多! 如果两者都存在,那么
sizeof(struct name_of_struct)
会让你得到类型的大小,而不是对象的大小,而两者
sizeof(name_of_struct)
和
(sizeof name_of_struct)
将为您提供对象的大小。
据我所知:区别在于 c 样式结构表示法与C++表示法。
相关文章:
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- C++Union/Struct位域的实现和可移植性
- 如何在c++中定义以struct为数据成员的类中的构造函数
- 结构体 S { int align; } 之间的区别;(struct 关键字后的名称)和 struct { int al
- struct.error:解压缩 C++ 结构时,解包需要 288 字节的缓冲区
- 如何使我的 sizeof sum 结构与空参数包一起工作
- 同一对象的"sizeof"的不同答案
- 错误:"Left of getValue must have class/struct/union"
- 在 SVM-Struct 中未定义对 sqrt 的引用(已使用 -lm)
- 数据类型"struct seq<0, 1, 2>{}"含义是什么?
- 为什么数组大小信息可用于"sizeof"运算符和 delete[] 运算符,但在将数组作为参数传递到
- 为什么 sizeof 在 C++ 中给出不正确的字节数?
- sizeof(size_t) 可以小于 sizeof(int) 吗?
- sizeof(* struct 指针)是否给你结构的值
- sizeof(struct name_of_struct) vs sizeof(name_of_struct) 之间的区
- sizeof(struct)C++中奇怪的输出
- sizeof(*this) and struct inheritance
- sizeof(struct)返回的值比预期值小
- Sizeof (struct)返回意外值