Dynamically allocating 3D array
Hi, Am I doing this right? the following code is suppose to allocate memory of type GLubyte which used to hold all the values which are read from ppm file. But for some reason the image donst get displayed i suspect that I am not allocating the memory properly. Just to clarify is there any problem with the way I am allocating 3D array. Any comments on this code would be appreciated as well
Code:
int AllocateMemory( void )
{
unsigned int Count, Count1;
if( ( IndiaMapPPM = malloc( sizeof(GLubyte **) * Width ) ) != NULL )
{
for( Count = 0; Count < Width; Count++ )
{
if( ( IndiaMapPPM[Count] = malloc( sizeof( GLubyte *) * Height ) ) != NULL )
{
for( Count1 = 0; Count1 < Height; Count1++ )
{
if( ( IndiaMapPPM[Count][Count1] = malloc( sizeof(GLubyte) * RGB ) ) == NULL)
{
printf("Error: Malloc failed\n");
return 1;
}
}
}
else
{
printf("Error: Malloc failed\n");
return 1;
}
}
}
else
{
printf("Error: Malloc failed\n");
return 1;
}
return 0;
}
Thank you
ssharish