1

Let's say I have this to create a multidimensional array dynamically:

int* *grid = new int*[gridSizeX];

for (int i=0; i<gridSizeX; i++) {
  grid[i] = new int[gridSizeY];
}

Shouldn't be possible now to access elements like grid[x][y] = 20?

3

2 Answers 2

3

Yes, this should work fine.

But... you might want to consider using standard containers instead of manually managing memory:

typedef std::vector<int> IntVec;
typedef std::vector<IntVec> IntGrid;
IntGrid grid(gridSizeX, IntVec(gridSizeY));

grid[0][0] = 20;
Sign up to request clarification or add additional context in comments.

Comments

0

Yes - but in C/C++ it will be laid out as grid[y][x].

1 Comment

Pardon me - I mean it will be laid out in memory as grid[y][x]. And agree with gf - in C++ you may want to use standard containers, as then you can check their .size() and avoid any slipups :)

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.