dynamic multi-dimensional arrays - not vectors

I have a question regarding the creation and manipulation of multi-dimensional dynamic arrays. I specifically don't want to use vectors - i'm comfortable using them, but i have good reasons for doing what i'm doing......

Anyway - i'm creating an m X n array (NumberOfPoints X choose_k), with the following bit of code.
1
2
3
4
5
6
7
8
9
	int **nnArray = NULL;

  nnArray = new int*[NumberOfPoints];
  
  for(int i = 0; i < mNumberOfPoints; i++)  // this creates the m X n array
		nnArray[i] = new int[choose_k];

delete [] nnArray; // make sure to delete when finished with it......


My question is - what are the dimensions of the array, if we say NumberOfPoints = 100, and choose_k = 5

Do we have a 100 X 5 array, or a 5 X 100 array? Does it make a difference (i would have thought so, yes!!)

So, if i wanted to access the element relating to NumberOfPoints = 60, and choose_k = 3, what would my syntax be?
1
2
nnArray [60][3] = something; // OR
nnArray [3][60] = something; // This one? 


It would be a pointer to an array [NumberOfPoints] of pointers, each of which pointing to an array [choose_k] ( so 100x5 )

You are not deleting the whole memory on line 8 ( see http://www.cplusplus.com/forum/articles/7459/ )

It may be easier to handle it as a single array:
1
2
3
4
5
6
7
int *nnArray = new int  [ NumberOfPoints*choose_k ];

nnArray [ x + y*choose_k ]; // access element x, y as NumberOfPoints x choose_k array

nnArray [ x + y*NumberOfPoints ]; // access element x, y as choose_k x NumberOfPoints array

delete[] nnArray; // now this will free the whole memory used by nnArray 
Thank-you.
Topic archived. No new replies allowed.