Jump to content
  • Advertisement
Sign in to follow this  
MadsGustaf

allocating multidimensional arrays?

This topic is 4077 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i know how to create an multidimensional array, but not how to allocate it on the heap.
[source langg = "cpp"]
const int maxpoints = 250;
const int maxpixels = 5;

point* mousepoints = new point[maxpoints][maxpixels];

obviusly dosent work.

Share this post


Link to post
Share on other sites
Advertisement
You cannot allocate multidimensional arrays on the heap (because the first dimension should be fixed). You can use vectors-of-vectors or boost::multi_array in a much easier fashion.

Also, technically, new allocates memory on the free store (the heap is where malloc gets its memory from).

Share this post


Link to post
Share on other sites
Check the comp.lang.c FAQ.

int **array2 = malloc(nrows * sizeof(int *));
array2[0] = malloc(nrows * ncolumns * sizeof(int));
for(i = 1; i < nrows; i++)
array2 = array2[0] + i * ncolumns;



EDIT: Using "new" = C++ = use STL/Boost.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!