简体   繁体   English

创建结构的二维数组会导致崩溃

[英]Creating an 2-dimensional array of a struct results in crash

I am trying to generate a two-dimensional array of a struct , but this results in the program not launching. 我试图生成一个struct的二维数组,但这导致程序无法启动。 The window freezes and the program quits after a few seconds. 窗口冻结,程序在几秒钟后退出。 Any idea why? 知道为什么吗?

This is the file where I try to define the array cells . 这是我尝试定义数组cells

#ifndef _FIELD_H_
    #define _FIELD_H_

class Field {
    public:
        static const int minX = -400;
        static const int maxX = 400;
        static const int minY = 0;
        static const int maxY = 400;

        Field();

    private:
        struct absCell {
            int material;
            float health;
        } cells[maxX - minX + 1][maxY - minY + 1];
};

#endif

The program is able to run when I remove these four lines: 当我删除这四行时,程序能够运行:

        struct absCell {
            int material;
            float health;
        } cells[maxX - minX + 1][maxY - minY + 1];

Any idea how this happens? 知道这是怎么回事吗? Any help is appreciated! 任何帮助表示赞赏!

Update 更新

Okay, apparently the problem is that this array gets quite big. 好吧,显然问题是这个阵列变得非常大。 Maybe you can help me to optimize this. 也许你可以帮我优化一下。

Material has to be an int between 0 and 10. Health has to be a float between 0 and 1 with a maximum of 2 fractional digits. 材质必须是介于0和10之间的int。运行必须是0到1之间的浮点数,最多2个小数位。

How can I limit the size of these vars? 如何限制这些变量的大小?

Update 2 更新2

Mark B suggested the use of vectors while itwasntpete suggested the use of pointers, new and delete. Mark B建议使用vectors而itwasntpete建议使用指针,new和delete。 Where is the difference, what are the advantages and disadvantages of these two methods? 差异在哪里,这两种方法的优点和缺点是什么? Thanks again! 再次感谢!

you are allocating 801 * 401 (=321201) elements of struct absCell on the stack. 你是在堆栈上分配struct absCell 801 * 401(= 321201)个元素。 assuming you have a 32bit machine it is 2569608 Byte (~2.45 MB). 假设您有32位机器,它是2569608字节(~2.45 MB)。 this is blowing up your stack. 这会炸毁你的筹码。 see here . 看到这里

move the elements to heap like: 将元素移动到堆,如:

class Field {
public:
    static const int minX = -400;
    static const int maxX = 400;
    static const int minY = 0;
    static const int maxY = 400;

    Field() {
        cells = new absCell*[maxX - minX + 1];
        for(int i=0; i<maxX - minX + 1; i++)
            cells[i] = new absCell[maxY - minY + 1];
    }

    ~Field() {
        for(int i=0; i<maxX - minX + 1; i++)
            delete[] cells[i];

        delete[] cells;
    }

private:
    struct absCell {
        unsigned char material;
        unsigned char health;
    }**cells;
};

should solve your problem. 应该解决你的问题。

since your update material and health can be saved in a char . 因为您的更新材料和健康状况可以保存在char accessing health you have to recalculate it: 访问健康,你必须重新计算它:

put -> x*100
get -> x/100

and dont forget to cast it. 并且别忘了施展它。

Based on update note: 根据更新说明:

The first thing you can do is easily optimize your struct for space by using unsigned char for each attribute, using a fixed-point representation for your existing float (for example 0.23 would be stored as the integer 23). 您可以做的第一件事就是通过对每个属性使用unsigned char来轻松优化结构空间,使用现有float的定点表示(例如,0.23将存储为整数23)。

Then store the structs on the heap with vector instead of an array: 然后使用向量而不是数组将结构存储在堆上:

    struct absCell {
        unsigned char material;
        unsigned char health;
    };
    std::vector<std::vector<absCell> > cells_;

Then set up the constructor: 然后设置构造函数:

Field() : cells_(maxX - minX + 1, std::vector<absCell>(maxY - minY + 1)) {}

My guess is you're hitting the stack limit. 我的猜测是你达到了堆栈限制。 When you make that array like it is its trying to put 801*401*8 bytes on your stack. 当您创建该数组时,它试图在您的堆栈上放置801 * 401 * 8字节。 I did a quick compile and was having crashes until I lowered the numbers for your various mins and maxes. 我做了一个快速编译并且崩溃直到我降低了你的各种分数和最大值的数字。

If I got your snippet right way, you're trying to create an array. 如果我正确地获得了你的代码片段,你就是在尝试创建一个数组。 Allocation of the array is stack-based, with more than 2 Mb size required. 阵列的分配是基于堆栈的,需要超过2 Mb的大小。 There may be a chance that your stack has less than that 2Mb available, thus the code crashes right at the moment you're entering the function. 您的堆栈可能有少于2Mb可用,因此代码在您进入该函数时崩溃。

Try to allocate the same array dinamically. 尝试以dinamically方式分配相同的数组。

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

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