0

i am totally new to malloc and i have to create a 3d array for a project.i read in description of malloc that you have to assign a +1 size for null ending byte..but i am not understanding where??? below is my block of code....

array = malloc(row*sizeof(int**));
for(i=0;i<row;i++)
{
 array[i] = malloc(column*sizeof(int*));
 if(array[i]==NULL)
   {
    printf("out of space");
    return 1;
   }
 for(j=0;j<column;j++)
  {
   array[i][j] = malloc(3*sizeof(int));
   if(array[i][j]==NULL)
   {
    printf("out of space");
    return 1;
   }
  }

then i want access using array[1][1][1]...and so on....

if i use upto the column to assign some values..then in next if there are two fopen statement i am getting below error... i need fopen statements to read two files...

testImage: malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.
Aborted

But if i assign values to column-1 then the error is not there.....

Please help me here.....

Edited Details: This error i only get in ubuntu if i run it in unix i am not getting this error.... Sorry i will upload full code in 9 hours...(i dont have with me right now...sorry)...

yea i know it is poor emulation of 3D array but i just need it here... Let me define steps to you... First i define the array with above code. Then i assign values to each of the elements of the array...so i can manipulate it later...here if i assign values to every element using for loops in which if i include element array[row][column][1] = 23; Then after that if i open two files using fopen then the above error is generated..but if i open only one file using fopen then there is no problem....

Vivek
  • 79
  • 9
  • 1
    I prefer `malloc(row * column * 3 * sizeof(int))` and `arr[row * r + 3 * c + i]`. This way the memory is allocated in one big block instead of `row * col` tiny blocks. – japreiss Dec 19 '14 at 18:52
  • How you use `array[0][0][0]` and so on ... – chux - Reinstate Monica Dec 19 '14 at 18:52
  • 1
    Rather than say "if i use upto the column to assign some values..then in next if there are two fopen statement" "i assign values to column-1", post the code. – chux - Reinstate Monica Dec 19 '14 at 18:54
  • 2
    *"I read ... you have to assign a +1 size for null ending byte."* That is when the array is storing 0-terminated strings. If the string max length is say 16, then it needs 17 bytes of storage. This is one of the most common errors in static and dynamic array allocation for strings. But here you are allocating memory for `int`. – Weather Vane Dec 19 '14 at 18:59
  • @japreiss did you mean `arr[(column * r + c) * 3 + i]`? – Weather Vane Dec 19 '14 at 19:14
  • the initial call to malloc() needs the returned value check. a good/clean method would be: if( NULL == (array = malloc( row*sizeof(int**)))) { ... Suggest using perror() rather than printf() as then the actual reason for the failure would output. – user3629249 Dec 19 '14 at 20:02
  • arrays, in C, are referenced in the range 0...(number of entries-1). So if array[10] then allowed access is array[0] ... array[9]. I.E. array[10] is accessing beyond the end of the array, accessing beyond the end of the array is undefined behaviour and can lead to a seg fault event. – user3629249 Dec 19 '14 at 20:10
  • 1
    This is *not* a 3D array, but only a poor emulation of it. See the question that @Peter pointed to for real multidimensional arrays in C. In your case for dynamic arrays, since C99 you could define `int (*array)[m][k] = malloc(sizeof(int[n][m][k]));` – Jens Gustedt Dec 19 '14 at 20:45
  • ... or `int (*array)[m][k] = malloc(n * sizeof(*array))`. Either works if VLA's are supported by your toolchain. – WhozCraig Dec 19 '14 at 23:32

0 Answers0