简体   繁体   English

在结构中设置数组元素时出现总线错误

[英]Bus error when setting array element in struct

I have the following struct:我有以下结构:

struct State {
    int num_walls;
    Wall walls[];

    int num_persons;
    Person persons[];
};

I want to have persons contain two Persons and walls contain one Wall:我想让人包含两个人,而墙包含一个墙:

int num_walls = 1;
int num_preson = 2;

State simState = *(State*)malloc( sizeof(simState) + num_person*sizeof(Person) + num_walls*sizeof(Wall));
simState.num_walls = num_walls;
simState.num_persons = num_person;
simState.walls[0] = w;
simState.persons[0] = p1;
simState.persons[1] = p2;

When I do that I get Bus error (core dumped).当我这样做时,我得到总线错误(核心转储)。 When I only set the persons, everything works fine.当我只设置人员时,一切正常。 Ie this works:即这有效:

int num_walls = 0;
int num_preson = 2;

State simState = *(State*)malloc( sizeof(simState) + num_person*sizeof(Person) + num_walls*sizeof(Wall));
simState.num_walls = num_walls;
simState.num_persons = num_person;
// simState.walls[0] = w;
simState.persons[0] = p1;
simState.persons[1] = p2;

The code is c++11 and I'm using gcc (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609代码是 c++11 我正在使用 gcc (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609

Why does that happen?为什么会这样?

So this is legal C++, it may be what you are trying to achieve所以这是合法的 C++,它可能是你想要实现的

struct State {
    int num_walls;
    Wall* walls;
    int num_persons;
    Person* persons;
};

State simState;
simState.num_walls = num_walls;
simState.walls = new Wall[num_walls];
simState.num_persons = num_person;
simState.persons = new Person[num_person];
simState.walls[0] = w;
simState.persons[0] = p1;
simState.persons[1] = p2;

It's not good C++, but at least it's legal. C++虽然不好,但至少是合法的。

The important points (in either language) is that you do not have to allocate simState , it gets it's memory just by being declared, but you do have to allocate your two variable length arrays simState.persons and simState.walls重要的一点(在任何一种语言中)是您不必分配simState ,只需声明它就可以得到 memory ,但是您必须分配两个可变长度 arrays simState.personssimState.walls

It's not allowed to define dimensionless arrays in C++, you should be using pointer with allocation (new or even malloc) as stated by others.不允许在 C++ 中定义无量纲 arrays,您应该使用其他人所说的分配指针(新的甚至是 malloc)。

Dimensionless arrays are valid construct in C99, it's not called VLA but flexible array member .无量纲 arrays 是 C99 中的有效构造,它不称为 VLA,而是灵活的数组成员 This feature of language can be used only as last member of structure with several other restrictions on definition and usage.语言的这一特性只能用作结构的最后一个成员,并在定义和使用方面有一些其他限制。 Definitely not regularly used feature.绝对不是经常使用的功能。

Reason for "Bus error" on first example is illegal definition of wall it's not last member of struct.第一个示例中“总线错误”的原因是的非法定义,它不是结构的最后一个成员。 Since person is correctly defined you are accessing memory past structure and overwriting something else which is really not what you want to do.由于person被正确定义,您正在访问 memory 过去的结构并覆盖其他实际上不是您想要做的事情。 GCC is reason why your program compiles even when it's not valid since lot of extensions is enabled by default. GCC 是您的程序编译的原因,即使它无效,因为默认情况下启用了许多扩展。 Please use -pedantic flag together with version of C++ standard (-std=c++11) to get warning about that.请使用-pedantic标志和 C++ 标准版本 (-std=c++11) 来获得有关警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM