I'm facing a very annoying problem, which is; I am allocating a triple pointer like this:
char*** allocTripleCharPtr(int firstDim, int secondDim, int thirdDim)
{
int i = 0;
int j = 0;
char*** triplePointer = malloc((firstDim*sizeof(char**)));
if(triplePointer == NULL) return NULL;
for(i = 0; i < firstDim; i++)
{
triplePointer[i] = malloc((secondDim+1)*sizeof(char*));
if(triplePointer[i] == NULL) return NULL;
}
for(i = 0; i < firstDim; i++)
{
for(j = 0; j <= secondDim; j++)
{
triplePointer[i][j] = malloc(thirdDim*sizeof(char));
if(triplePointer[i][j] == NULL) return NULL;
}
}
return triplePointer;
}
and the dimensions aren't limited (right now, third dim is always 3).
If the program gets called with dimensions such as 30000*30000*3, the whole Computer simply crashes, not just the terminal (using Ubuntu 64bit, GCC).
I've tried limiting the array by inserting if(first dim > (INTMAX/seconddim)) return ERROR;
hoping it has to do with an overflow, but no luck there.
So my questions are - first, why does it crash? It works with 40k * 2k, but not 40k * 20k.
and second, how much memory can be allocated, and are there allocation errors that aren't detected by if(triplePtr == NULL) return ERROR;
?
Thank you!
(edit: I know that sizeof(char) is always one, but that's the way I need to write it for this assignment. Sorry about that.)