Problem in Free() on 2D array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • manjuks
    New Member
    • Dec 2007
    • 72

    Problem in Free() on 2D array

    Hi,

    Please see the below code snipet, its giving segmentation fault while freeying array[i]. Anybody help in whats going wrong?

    Code:
    void deallocate2D(int** array, int nrows) {
    
         /*  deallocate each row  */
         int i;
         for(i = 0; i < nrows; i++) {
              printf("deallocate each row %d\n",i);
              free(array[i]);
         }
    
         /*  deallocate array of pointers  */
         printf("deallocating array of pointers\n");
         free(array);
    
    }
    Thanks in adavance

    Manjunath
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    For what value of i?

    Did you actually allocate the memory in the way you are trying to free it?
    Have you called deallocate2D twice by accident?
    Have you call deallocate2D with the correct value for nrows?

    free generally only fails in that way if the pointer given is not a valid heap pointer i.e. a pointer to somewhere other than the heap or a pointer into the heap that has already been freed or 0.

    Comment

    • manjuks
      New Member
      • Dec 2007
      • 72

      #3
      Hi Bafna,

      Thanks for the reply...
      Please have alook into the below full program.

      Code:
      #include<stdio.h>
      #include<stdlib.h>
      
      void allocate2D(int** array, int nrows, int ncols) {
      
           /*  allocate array of pointers  */
           printf("Allocating memory for array of pointers\n");
           array = ( int** )malloc( nrows*sizeof( int* ) );
      
           /*  allocate each row  */
           int i;
           for(i = 0; i < nrows; i++) {
                printf("Allocating memory for each row %d\n",i);
                array[i] = ( int* )malloc( ncols*sizeof( int ) );
           }
      
      }
      
      void deallocate2D(int** array, int nrows) {
      
           /*  deallocate each row  */
           int i;
           for(i = 0; i < nrows; i++) {
                printf("deallocate each row %d\n",i);
                free(array[i]);
           }
      
           /*  deallocate array of pointers  */
           printf("deallocating array of pointers\n");
           free(array);
      
      }
      
      int main()
      {
        int **arr;
        allocate2D(arr,2,2);
        deallocate2D(arr,2);
        return 0;
      }
      I am not able to understand where its going wrong. Please help me in understnding the problem.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        You have not returned the value of pointer allocated in allocate2D to main, the pointer arr in main remains uninitialised and then you pass it to deallocate2D which tries to free the uninitialised pointer that is passed to it.

        I suggest you return the pointer allocated in allocate2D via the return value of the function. Alternatively you need to pass the first parameter of the function allocate2D as int***. Remember C is pass by value, if you aren't dereferencing a pointer passed into a function you are only working on local data.

        Comment

        • manjuks
          New Member
          • Dec 2007
          • 72

          #5
          Thanks a lot Bafna. Now I understood whats the problem, and solved it.
          Thank you very much.

          Thanks
          Manjunath

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            Furthermore, allocate2D should be testing the return value from malloc each time. If your computer runs out of memory in the middle of allocate2D execution then you end up with a partially allocated 2D array, which will cause all kinds of problems for you.

            If allocate2D gets an error from malloc it should deallocate the partially allocated 2D array and return NULL. Your main program should test the return value of allocate2D.

            deallocate2D can be used to deallocate a partially allocated 2D array if you test each pointer for NULL before calling free.

            Comment

            Working...