Dynamic Memory

Started by
2 comments, last by Sneftel 17 years, 1 month ago
Hey there. I'm fairly competant with C++ but rather new to dynamic memory. Basically I am making a minesweeper game which, as you know, can be of any size set by the user. So that's normal arrays out. The problem with ordinary dynamic memory is it's 1 dimensional, although I suppose I could find mathmatical ways to find out the correct squares. So basically, does anyone have any advice and hints as to the best way to create a minesweeper board of an unknown size? I will be needing to send this board as a parameter to other parts of the program. Thanks. Neil.
Advertisement
When you think "C++" "array" and "dynamic", the next word you think should be "std::vector".

In this case, a std::vector of std::vectors.

E.g.

typedef std::vector<Cell> BoardRow;
typedef std::vector<BoardRow> Board;
I think the easiest way here is just to allocate a standard array on dynamic memory.

// field dimensions
int width, height;
// field access coords
int x, y;

// create field array
int *field = new int[ width * height ];
// access field square
field[ x + y * width ];
// delete field array
delete[] field;
Moved to For Beginners.

This topic is closed to new replies.

Advertisement