0

For example this is the codes for a function file, named fnx.c, where func() will be called from the main function. How should I free the TempArray here while return the double pointer to the main function at the same time?

    int * * TempArray; //global variable in this file

static void CreatePointer(int row, int col) {
    int r;
    TempArray = malloc(sizeof(int * ) * (row));
    for (r = 0; r < row; r++) {
        TempArray[r] = malloc(sizeof(int) * (col));
    }
}


static void DestroyPointer() {
    free(TempArray);
    TempArray = NULL;

}

int * * func(int * * OriArray, int row, int col, int r_offset, int c_offset) {
    int r, c;

    CreatePointer(row, col);

    for (r = 0; r < row; r++) {
        for (c = 0; c < col; c++) {
            TempArray[r][c] = OriArray[r + r_offset][c + c_offset];
        }
    }

    //destroy TempArray??

    return TempArray;

}
Jamie Eltringham
  • 810
  • 3
  • 16
  • 25
randomGirl
  • 21
  • 7
  • the `DestroyPointer` function is incorrect. Also why would you delete a pointer after you allocate and fill the memory? – bolov Jan 29 '16 at 09:03
  • @bolov Hello there, thanks for the reply. How should I do the Destroy Pointer function then? I thought it's very direct like what I did here. Actually I was asked by my supervisor to free every pointers after I use the malloc(), the flow is something like the one in func(), but I do not get the idea how to do it. – randomGirl Jan 29 '16 at 09:06

3 Answers3

3
  1. In your main(), call CreatePointer()
  2. In your main(), call func()
  3. Outside func(), when you don't need TempArray anymore, call DestroyPointer()

And your DestroyPointer() is wrong. You'll have memory leaks. It should be something like :

static void DestroyPointer (int row)
{
    for (int r = 0; r < row; r++)
    {
       free(TempArray[r]); 
    }
    free(TempArray);
}
Pierre
  • 1,162
  • 12
  • 29
  • Hi @Pierre , what and where do you mean by (outside main()) actually? Sorry If I am asking stupid question. – randomGirl Jan 29 '16 at 09:16
  • 1
    I understand the flow now. Please ignore my previous comment (I deleted). Thanks for your answer again! – randomGirl Jan 29 '16 at 09:41
  • No you can't do like this, because you'll have problem if you dereference a pointer after which has been free. For now and forever, try to follow this rule : the function/class/scope which frees a pointer should be the same which has allocated it. – Pierre Jan 29 '16 at 09:41
1

if you want to use this point(TempArray) after called from the main function,you can not destroy it(TempArray).Only when you do not want to use this point datas,you can destroy it. you can test like this:

main()   
{   
  int *p = func(oriArray,row,col,r_offset,c_offset);   

   for (int r = 0; r < row; r++){   
       for(int c = 0; c < col; c++){   
         printf("%d",p[r][c]);   
      }   
   }      

  DestroyPointer();   
}

Hope it helps !

arrfu
  • 182
  • 7
0

You should free all the rows before freeing up TempArray.

static void DestroyPointer (int row)
{
   for (int r = 0; r < row; r++)
   {
      free(TempArray[r]);
   }
   free(TempArray);
   TempArray = NULL;

}

But You shouldn't destroy the pointer before returning it,
because dereferencing it after that in main will cause undefined behaviour.

Viktor Simkó
  • 2,607
  • 16
  • 22