# 3d array allocation

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-21-2008
C_ntua
3d array allocation
Would this work?
Code:

`int (*p)[x][y] = malloc(z * sizeof(*p));`
If I understand correctly, p is a pointer to a 2D array and sizeof(*p) will be sizeof(int)*x*y?
• 10-21-2008
master5001
You are correct... but you tell me if z is appropriately sized?
• 10-21-2008
master5001
Wait... I just noticed your title. Let me ask you this, is this a 3d array?
• 10-21-2008
C_ntua
I suppose. I mean since you have a pointer and the appropriate space you could do:
Code:

```for (int i=0; i<z; ++i)   for (int j=0; j<y; ++j)       for (int k=0; k<x; ++k)           printf("%d", p[k][y][i]; //or p[i][y][k]?```
• 10-21-2008
Salem
The loop limits would be z, x, y
The subscript would be [i][j][k]
• 10-22-2008
C_ntua
Quote:

Originally Posted by Salem
The loop limits would be z, x, y
The subscript would be [i][j][k]

Yeah, typed it wrong. But to get this straight.... would my first code malloc() a 3rd array?
• 10-22-2008
matsp
Not on it's own, no.

It allocates ONE array element of z dimension. you need x * y of them, each one installed in the respective [x][y] pointer.

--
Mats
• 10-22-2008
C_ntua
Code:

`int (*p)[x][y] = malloc(z * sizeof(*p));`
So how much space does this allocates?
Since p is a pointer to a 2D array doesn't that mean that sizeof(*p) is the size of the 2D array, thus x*y*sizeof(int) ?
If that is true, then the above will allocate z*y*x*sizeof(int) space, thus space for a 3D array.

I could do of course:
Code:

```int ***p = malloc(z * sizeof(*p)); for (...) {   p[...] = malloc(...)   for (...)       p[...][...] = malloc(...) }```
and allocate a 3d array, but I am just wondering if the one line code would work, since it is simpler and guarantees a contiguous space of memory.

If it won't work, I would like to know what would happen. The pointer to a 2D array syntax is not that clear in C
• 10-22-2008
matsp
A 3D that uses dynamic allocation will need to allocate one chunk of memory for each [x][y] location, so even if you avoid doing the first two levels of allocation, you still need to use a loop to fill in the bottom layer of the pointers.

--
Mats
• 10-22-2008
C_ntua
Lets say you allocate one array. You would do:
Code:

`int *p = malloc(z*sizeof(int));`
So you have this:
p--> M1
The pointer points to one int, so you just allocate z int and have an array.
p--> M1 ..... Mz

If you had though (*p)[3][3] wouldn't that meant that you have this:
p--> M1 M2 M3 M4 M5 M6 M7 M8 M9
So if you do what I posted you should have z times that.

Maybe I am not understanding the (*)[][] syntax correctly?

So you are saying that my line would be the first line of code to allocate and I would need a double loop to fill the array?
• 10-22-2008
matsp
It's not about the amount of data you allocate, but rather how it is distributed in the array.

To make things simpler, let's just use a 2D array:
Code:

```int (*p)[3]; p[0] = malloc(3 * sizeof(int));```
Now, you have a 2D array, but only one set of second level data, p[0][0..2] is available.

To make that a proper 2D array, you would need to do:
Code:

```for(i = 0; i < 3; i++)   p[i] = malloc(3 * sizeof(int));```
Likewise, you need to allocate x * y blocks of z elements to make a 3D array, since each pointer p[x][y] is an individual pointer, and thus needs to point to the z elements it represents.

--
Mats
• 10-22-2008
C_ntua
Yes, but if you do this:
Code:

```int (*p)[3]; p = malloc(3 * sizeof(int[3]);```
which I assume is equivalent with this:
Code:

```int (*p)[3]; p = malloc(3 * sizeof(*p);```
The thing is that I can understand how pointers work. But int*** p is no the same as int p[][x][y]. How the second is implemented is not really clear. But I understand that if you do this:
Code:

```int a[2][2][2]; int (*p)[2][2] = a; p[0][0][1] = 2;```
then p points to a and you can use the indexes correctly.
So you point p to a chunk of memory that has 8*sizeof(int).
Instead of that, can't you dynamically allocate that piece of memory?

I should use this example which is more to my point, for a 2D array:
Code:

`char (*p)[10] = malloc(100);`
So you have a pointer that points to a chunk of 100byts. Shouldn't the indexes work as you expect?
• 10-22-2008
C_ntua
Am I just missing an index here? I mean would this work?
Code:

`char (*p)[10][10] = malloc(100);`
And use it like:
Code:

`p[0][0] = '1'; p[1][0] = '2'; etc etc`
• 10-22-2008
Salem
> It allocates ONE array element of z dimension. you need x * y of them, each one installed in the respective [x][y] pointer.
But that's what the sizeof(*p) in the original post tells you surely.
It should give you the same result as sizeof( int[x][y] ).

IMO, int (*p)[x][y] = malloc(z * sizeof(*p)); does what you want, so long as x and y are compile-time constants.
• 10-22-2008
Salem
> It allocates ONE array element of z dimension. you need x * y of them, each one installed in the respective [x][y] pointer.
But that's what the sizeof(*p) in the original post tells you surely.
It should give you the same result as sizeof( int[x][y] ).

IMO, int (*p)[x][y] = malloc(z * sizeof(*p)); does what you want, so long as x and y are compile-time constants.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last