Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
189 views
in Technique[技术] by (71.8m points)

c++ - 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. 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.

#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.

How can I limit the size of these vars?

Update 2

Mark B suggested the use of vectors while itwasntpete suggested the use of pointers, new and delete. Where is the difference, what are the advantages and disadvantages of these two methods? Thanks again!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are allocating 801 * 401 (=321201) elements of struct absCell on the stack. Assuming you have a 32bit machine it is 2569608 Byte (~2.45 MB). According to this it is blowing up your stack.

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;
};

Update

Material and health can be saved in a char. Accessing health you have to recalculate it:

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

Update 2

Except you have some reasons, using a vector is most likely the better option, as Mark B 's answer describes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...