0

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.)

BigBadWolf
  • 603
  • 2
  • 8
  • 17
  • 1
    You return `NULL` when allocation fails. Just make sure you free the previously allocated memory before returning so as to keep a room for the stack for further processing. Also read [this post](http://stackoverflow.com/questions/2798330/maximum-memory-which-malloc-can-allocate) – Mohit Jain Feb 03 '15 at 11:51
  • 2
    It is very unlikely malloc will ever return NULL, unless overcommit is disabled. "Whole computer crash" is even less likely. You can catch SIGSEGV to determine page allocation fault, but it is, well, just hard. – keltar Feb 03 '15 at 11:54
  • Thank you! I do free the memory in case it fails, but I defined a lot of macros and didn't want to insert a gigantic code snippet in here. I'm reading that post right now, interesting stuff. Haven't found a solution yet, but maybe there really isn't a complete solution. – BigBadWolf Feb 03 '15 at 11:55
  • 1
    @BigBadWolf: my guess is you have a bug in your macro-based freeing code. The code you posted looks correct and should work, given the memory is properly freed. – morfizm Feb 03 '15 at 11:57
  • 1
    I would suggest representing your 3D array as a struct with a 1D flexible array member (malloc-ed once), and have some macros or inline functions to access & modify it using 3D coordinates. – Basile Starynkevitch Feb 03 '15 at 12:03
  • @BasileStarynkevitch That would make it simpler, but I need to avoid structs for portability. – BigBadWolf Feb 03 '15 at 12:15
  • 1
    @BigBadWolf: I can't understand why `struct` are less portable. – Basile Starynkevitch Feb 03 '15 at 12:16
  • 2
    @BigBadWolf - I agree with Basile here. `struct` has been portable since *before* C89, there are C89 compilers for nearly anything with a CPU. A single large array will also be more efficient to free and incur much less overhead memory cost (every `malloc` actually allocates some "hidden" memory to tell `free` where the memory goes when you're done with it). Might even be better from a cache performance standpoint. – Brian McFarland Feb 03 '15 at 12:37
  • @BasileStarynkevitch : I need to write the array to file, and every compiler optimizes structs in a different way. Since I'm learning to program for engineering, I will have to deal with compilers that don't accept #pragmas or __attribute__s, and won't write the exact bytes but may just optimize the 3bytes to 2, and then the file would be corrupt. At least that's what I'm told. BrianMcFarland: I've tried doing it with an array, but arrays seem to have a lower total size limit than dynamic allocation. – BigBadWolf Feb 03 '15 at 12:44
  • You just need to have a writing and a reading routine. Of course, writing a *textual* file is very portable. BTW, you could use some textual format like [JSON](http://json.org/) ; or you want some binary serialization format and library .... – Basile Starynkevitch Feb 03 '15 at 12:46
  • Thank you for the input, I will get into that. Sounds very interesting! But right now I will try to get my program to work as it is right now, since the whole program builds on the triple pointer and I do not have time to rewrite it all until the deadline. Should have asked earlier. :/ – BigBadWolf Feb 03 '15 at 12:50

0 Answers0