Sign in to follow this  

Dynamic Memory

Recommended Posts

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.

Share this post

Link to post
Share on other sites
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.


typedef std::vector<Cell> BoardRow;
typedef std::vector<BoardRow> Board;

Share this post

Link to post
Share on other sites
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;

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this