[prog] free() troubles, pointer arrays

ed orphan millward at Ms.UManitoba.CA
Wed Mar 17 17:54:34 EST 2004


I built this pointer array, allocated memory for each pointer,
and have a pointer to the pointer array so I can referrence
the array outside of the function that created it.
This is Linux GNU C.
It all works, except when it comes time to free the memory.
This is a brief summary of the code:
   char ** create_buffers( char ** main_ptr );
int main(void)
{     char * * main_ptr;
      main_ptr = create_buffers( main_ptr );
  display the contents and it all OK so everthing
  works fine, until it's time to free the memory.
      for(i=0; i<max_rows; i++)
           free( * main_ptr[i] );
      main_ptr = NULL;
      return 0;
}                   This compiles  with
 gcc -Wall -W -o program_name  program_name.c
  The executable runs just fine, except the compiler gives a warning:
"warning: passing arg1 of 'free' makes pointer from integer
  without a cast"
It is no good using a cast. The code will compile clean but results in a
segmentation error at run time.  Gdb point to an 'error in free() '
Just about everything else I've tried results in a segmentation
error at run time except the above code.
I do not like using code that compiles with warnings. Does anyone
know the correct way to free an array of pointers with memory allocated to
each pointer?   I must be missing something obvious.



More information about the Programming mailing list