# Thread: 3-dimensional array dynamic allocation

1. ## 3-dimensional array dynamic allocation

Hello everybody!

I'm trying to create a RGB picture of variable size using dynamic memory alloction. What I need to achieve is image[y][x] , where y is number of vertical pixels, x is number of horizontal pixels, 3 is number of colours: Red, Green, Blue.

Here is my code:

Code:
```int ***image;
image = (int***)malloc(Ycount*sizeof(int**));
for (Ycount=0; Ycount<YSIZE; Ycount++)
{
image[Ycount] = (int**)malloc(Xcount*sizeof(int*));
for (Xcount=0; Xcount<XSIZE; Xcount++)
image[Ycount][Xcount] = (int*)malloc(3*sizeof(int));
}

for(y = 0; y < Ycount; y++)     //putting data into the array
for(x = 0; x < Xcount; x++)
{
image[y][x][RED] = 255; // Value of red colour in the image
image[y][x][GREEN] = 0; // Value of green colour in the image
image[y][x][BLUE] = 0; // Value of blue colour in the image
}```
I think that memory allocation is successful, but something is wrong when putting data into the array is in process.

I'd appreciate any help. Thanks 2. Presuming Xcount and Ycount were initialized to correct values initially, your allocation should be fine (personally, I think it's better to use YSIZE and XSIZE in the malloc). What does "something is wrong when putting data in" mean?

Couple of observations:
1) You don't have to cast the return value of malloc() in C.
2) if these are 0-255 RGB values, maybe you should use unsigned char instead of int. This will save you 75% of the memory usage (0-255 RGB values are one byte each). 3. Post the definitions of RED, BLUE, and GREEN.
Are they placeholders for the 0-2 index range? 4. Code:
```image = (int***)malloc(Ycount*sizeof(int**));
for (Ycount=0; Ycount<YSIZE; Ycount++)
{
image[Ycount] = (int**)malloc(Xcount*sizeof(int*));
for (Xcount=0; Xcount<XSIZE; Xcount++)```
Since the red things are your loop variables (presumably garbage at this point), perhaps use the YSIZE and XSIZE respectively.

Ditto for the loops which follow, although those seem to be accidentally correct for the moment.

Also, see the FAQ on casting malloc. 5. Post the definitions of RED, BLUE, and GREEN.
Are they placeholders for the 0-2 index range?
In RGB picture each pixel consists of 3 colours: Red, Green and Blue, and the value is between 0 and 255 for each colour. Say, I want a pixel with coordinate (50, 50) to be Red, so I will have

Code:
```image[RED] = 255;
image[GREEN] = 0;
image[BLUE] = 0;```
What does "something is wrong when putting data in" mean?
Thanks for the reply, I've modernized my code
Code:
```          int XSIZE, YSIZE;
int ***image;
int x,y;
int Xcount, Ycount;

printf("Image width:");
scanf("%d", &Xcount);
printf("Array height: ");
scanf("%d",&Ycount);

image = malloc(YSIZE*sizeof(int**));
for (YSIZE=0; YSIZE<Ycount; YSIZE++)
{
image[YSIZE] = malloc(XSIZE*sizeof(int*));
for (XSIZE=0; XSIZE<Xcount; XSIZE++)
image[YSIZE][XSIZE] = malloc(3*sizeof(int));
}

for(y = 0; y < YSIZE; y++)     //putting data into the array
for(x = 0; x < XSIZE; x++)
{
image[y][x][RED] = 255; // Value of red colour in the image
image[y][x][GREEN]= 100; // Value of green colour in the image
image[y][x][BLUE]= 100;// Value of blue colour
}```
The main problem is when I start the programm. It builds with no error, but when I try to debug it gives an error and points an arrow to the row image[Ycount][Xcount][RED] = 255; 6. Okay. A few basic things.

Generally, all caps NAMES are used for defines, eg:
Code:
`#define XSIZE 200`
which is what I was presuming those were before. Anyway, so when I said use that with malloc, I mean that you should not use the value which changes in a for loop:
Code:
```	for (YSIZE=0; YSIZE<Ycount; YSIZE++)
{
image[YSIZE] = malloc(XSIZE*sizeof(int*));```
YSIZE and XSIZE are now going up and down, whereas Ycount and Xcount remain constant.

Altho, because of how the for() is, for every loop after the first one, it will be the correct value. However, right now it looks like XSIZE has not been defined yet for the first iteration of the Y loop! In any case, it's better to use a constant value here, which would now be Xcount. Xcount does not change after it has been set. Make sense? You should do the same thing in the second loop here:
Code:
`for(x = 0; x < XSIZE; x++)`
But that is not the exact cause of your problem. What is the "error" you are given (cut n' paste it here)?

Also: have you defined RED, GREEN, and BLUE? 7. > image = malloc(YSIZE*sizeof(int**));
> for (YSIZE=0; YSIZE<Ycount; YSIZE++)
Fine, just blow a hole in your other foot then.

It's the same problem rearranged - you're not allocating what you're trying to use. 8. Use a 2D array of structures? Use a 2D array of integers, and mask RGB (and hey, Alpha if you really want)? You don't really need 3Ds here unless you are just really bored.

Quzah. Popular pages Recent additions 3d array, dynamic allocation, malloc 