c++ - 具有灵活数组成员的结构的大小

标签 c++ c sizeof flexible-array-member

给定

struct Foo {
    uint32_t a;
    uint32_t b[];
};

什么是sizeof(Foo)?它是实现定义的还是未定义的行为? C 与 C++ 的答案是否不同?

最佳答案

编译器将忽略灵活数组成员,因为它不存在。

C11-§6.7.2.1 (p18)

[...] In most situations, the flexible array member is ignored. In particular, the size of the structure is as if the flexible array member were omitted except that it may have more trailing padding than the omission would imply [...].

据我所知,直到 c++14,灵活的数组成员才不是 C++ 标准的一部分。但是,GNU 支持它为 an extension . C 和 C++ 的行为相似。

关于c++ - 具有灵活数组成员的结构的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45193984/

相关文章:

c++ - vector<T> 的内存结构

c++ - `static_assert` 和构造函数中的顺序或错误

C 改变了一点 uint32_t 不工作

c - "sizeof"之后的赋值表达式从未执行过吗?

c - C 中结构数组的 'sizeof' 结果?

c++ - 对结构С++的误解

c++ - boost::spirit 编译问题与 BOOST_FUSION_ADAPT_STRUCT

c++ - 如何从旋转矩阵计算角度

c - 获取结构内部变量的偏移量是基于 NULL 指针,但为什么呢?

客户端发送 500 字节,但服务器接收 244 字节 - 套接字编程?