GCC 4.8.x 中处理灵活数组成员的错误
Bug in GCC 4.8.x Handling Flexible Array Member?
我有以下代码:
#include <cstdint>
struct parent
{
uint64_t id;
char data[];
};
struct child : public parent
{
uint32_t tmp;
char text[];
};
int main() {
child d;
d.id = 1;
}
当使用 GCC 7.2.1 编译时,它给了我错误:
flex.cpp:6:20: error: flexible array member ‘parent::data’ not at end of ‘struct child’
char data[];
^
flex.cpp:11:13: note: next member ‘uint32_t child::tmp’ declared here
uint32_t tmp;
^~~
flex.cpp:9:8: note: in the definition of ‘struct child’
struct child : public parent
^~~~~
使用 GCC 4.8.5 编译时,没有警告或错误就可以了。
GCC 4.8.5 中的错误?
提前感谢!
是的,这看起来像 GCC 4.8 中的一个错误。 子类使用的内存在超类的内存之后。 灵活数组成员在语法上位于超类的末尾,但不适用于整个对象的内存布局。 这类似于涉及组合的C案例:
struct parent
{
uint64_t id;
char data[];
};
struct child
{
struct parent parent;
uint32_t tmp;
char text[];
};
这也不是有效的 C,尽管 GCC 7 及更早版本只警告-pedantic
(在我看来这有点鲁莽(。
请注意,灵活的数组成员是 GNU 扩展,而不是 C++ 标准的一部分。
相关文章:
- 为什么我无法访问指向数组中成员函数的指针?
- 如何将结构/联合数组的成员传递到函数中
- 使用带有参数包的数组的成员数组初始化类
- 有没有办法给数组的成员指定不同的名称?
- 使用包含结构的数组的成员函数
- C++:释放动态数组(结构成员)和指向此结构的指针的方法
- C 用数组作为成员创建对象
- 用另一个 constexpr 数组对成员数组进行大括号初始化
- 来自数组的成员和比特菲尔德的结构
- 如何创建指向数组类成员的指针?
- 将数组的成员设置为零
- "Empty"数组\向量成员 C++
- 功能指针数组(包括成员功能)投掷模板专业化错误
- 另一个结构内部的结构数组的成员变量没有通过引用传递
- 正确转换 2D 数组的成员变量
- 常量数组类成员初始化
- 为具有对象或对象数组作为成员的类运行不同的代码
- C++ 如何指向数组的成员
- 将包含数组数据成员的结构保存到文件
- 数组类成员初始化(C++)