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

后端 未结 4 1006
抹茶落季
抹茶落季 2021-01-07 01:14

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

4条回答
  •  攒了一身酷
    2021-01-07 01:28

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

    Then store the structs on the heap with vector instead of an array:

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

    Then set up the constructor:

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

提交回复
热议问题