allocating multidimensional arrays?

Started by
1 comment, last by CadetUmfer 16 years, 7 months ago
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.
•°*”˜˜”*°•.˜”*°•..•°*”˜.•°*”˜˜”*°•..•°*”˜˜”*°•.˜”*°•.˜”*°•. Mads .•°*”˜.•°*”˜.•°*”˜˜”*°•.˜”*°•..•°*”˜.•°*”˜.•°*”˜ ˜”*°•.˜”*°•.˜”*°•..•°*”˜I am going to live forever... or die trying!
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).
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.
Anthony Umfer

This topic is closed to new replies.

Advertisement