Question on memory allocation for multi-dimensional array

S

sugaray

I don't get why the correct way to allocate memory space for
3d-array should be like this in C++:

long (*ptr)[10][15];
ptr=new long[5][10][15];

and when deallocation

delete[] ptr;

Why declare the last two dimensions (*ptr)[10][15] first ?
Why not
long (*ptr)[5][10];
or
long *ptr=new long[5][10][15];

And why deallocate an 3d-array only use

delete[] ptr;
not
delete[][][] ptr;

Can anybody shed some light on the questions above ?
I'm new to C++ and is eager to get a better understanding of it,
Your help would be appreciated. Thanx in advance :)


Gary.
 
R

Rob Williscroft

sugaray wrote in
I don't get why the correct way to allocate memory space for
3d-array should be like this in C++:

long (*ptr)[10][15];
ptr=new long[5][10][15];

Perhapse it would help to understand that a C/C++ multi-dimension array
is really a thin vinear over a single-dimension array.

For example int a[10][5]; is implemented as a single block of storage
with 50 contiguous elements, a bit like int a_storage[50];.

Then when you reference a[n] the compiler translates the expression
a[n] to a_storage[ n * 10 ] (10 being the first declared dimension).
The type of the resulting expresion is int *, so a[n][m] is evaluated
as (a[n])[m] which is translated to a_storage[ (n * 10) + m ] with
type int.


Example 2: int b[2][3][4]; stored as int b_storage[ 2 * 3 * 4 ];
b[n] evaluates as b_storage[ n * 3 * 4 ] the type it evaluates to
is int (*)[4], b[n][m] evaluates to b_storage[ (n * 3 * 4) + (m * 4) ]
the type it evaluates to is int *. b[n][m][o] evaluates to
b_storage[ (n * 3 * 4) + (m * 4) + o ].
and when deallocation

delete[] ptr;

The compiler need's to know that ptr was initialized with a new []
expression, this is so it can find out in some way how many elements
were allocated (the new long[5][10][15] expression stored this
information (5 * 10 * 15 == 750 longs) when it allocated the memory).
This is so the compiler can loop over the array calling the destructor
for each element. Note that it only needs the total number of elements
as even though the array is multi-dimensional its stored as a single
dimensined array.
Why declare the last two dimensions (*ptr)[10][15] first ?

This is a result of the way pointer (and thus array) arithmatic is
done in C/C++.
Why not
long (*ptr)[5][10];
or
long *ptr=new long[5][10][15];

This simply won't work as it isn't possible to evaluate ptr[n]
as the size of the object (sub-array) it referes to is not part
of the type of ptr. In the example the sub-array is 10 * 15 long's
in size, but that information is *not* in the declaration of ptr.
And why deallocate an 3d-array only use

delete[] ptr;
not
delete[][][] ptr;

As explaned above all the compiler need to know is wether its deleteing
a single object (delete ptr) or an array (delete[] ptr).

HTH.

Rob.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top