2

I've got a game that has a grid, which is a 2D array. This array is filled with a struct of information.

struct GridCell
{
    uint mCellID;
    Vector2 mPosition;
    uint mLevel;
    int mCellType;
};

class Grid
{
public:
    Grid();
    ~Grid()

protected:
    // Heap
    GridCell[][] mGridCells;

    // Dynamic
    GridCell*[][] mGridCells;
};

Keeping in mind that there can be many Grids in memory at once, and that those gridcell can range from very small to very large: Is this better suited to be a heap of gridcells or dynamic (pointer) gridcells?

From what I understand: Heap cells will take up a large chunk of memory Dynamic cells still will, but it will be of pointers, not the entire struct. However, this could lead to fragmented memory?

I'm not sure of which one is the best for this scenario, and I probably don't fully understand the differences between the two either. Help?

2 Answers 2

3

Types in C++ have a fixed size. It's not true that your GridCells will range from very small to very large. They'll be one or the other. sizeof(GridCell) is fixed. They might each point to objects of different sizes, but those objects are not part of the size of the GridCell.

Either way, both methods you propose will have exactly the same amount of GridCells in memory, so you save nothing by using pointers. In fact, the dynamically allocated approach will use more memory because you're also storing pointers to each cell. The only way the pointer approach could be more memory efficient is if you used single GridCell objects to represent more than one cell in the Grid. That is, some of the pointers would be the same.

What this really comes down to is which one is more manageable and the answer is always "the method without the pointers". It means your Grid objects will automatically manage the construction and destruction of the GridCells without you having to care about it, helping you avoid memory leaks. If you use the pointer method, you will have to loop through your arrays in the constructor of Grid, doing new GridCell() for each element. You'll also need to do the same in the destructor, doing delete on each cell you dynamically allocated. This is a pain, especially when it's unnecessary.

In the cases where pointers are required, smart pointers are much more preferable.

Furthermore, you may even be better off using a std::array<std::array<GridCell, N> M> for your fixed sized arrays. It encapsulates the array for you, allowing you to use it as you would any other Container from the standard library.

Sign up to request clarification or add additional context in comments.

Comments

2

It would be better to use neither and use std::vector. Make your 2d arrays 1d and store the values straight into a vector. Or if you really must have 2 dimensional you can create a vector of vectors.

1 Comment

It may not be a concern here, but it is useful to note that a vector of vectors can lead to horrible performance if they grow large enough and need to be iterated relatively frequented. Locality of data is likely to be horrible. A better alternative then would be a large vector and manually calculating the offset to sub "arrays".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.