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][3] , 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