How should I free memory after I allow FETCH ALL statements in a C program to auto-allocate memory? I assume that it's OK to use a simple free statement to free memory allocated for an array of values (e.g., an array of float values). But what about memory allocated for and associated with an array of pointers (e.g., an array of pointers to character strings).

For example:

   long number_fetched ;
   long record ;

   exec sql begin declare section ;
   char  **char_column ;
   float  *float_column ;
   exec sql end declare section ;

   $declare fetch_cursor cursor for
   select char_column, float_column from my_table ;

   $open fetch_cursor ;
   $fetch all fetch_cursor into $char_column, $float_column ;
   $close fetch_cursor ;
   $free fetch_cursor ;

   number_fetched = sqlca.sqlerrd[2] ;

   for ( record = 0 ; record < number_fetched ; record++ )
      {
      // Do something with the fetched data ...
      printf ( "character string : %s   value : %f\n",
                char_column[record], float_column[record] ) ;
      }
   if ( char_column != NULL ) free (char_column ) ;
   if ( float_column != NULL ) free ( float_column ) ;

In this example should I free the memory allocated for each of the pointers in the char_column array before freeing char_column? :

for ( record = 0 ; record < number_fetched ; record++ )
  {
  free ( char_column[record] ) ;
  }

Thanks.


Chris

--
Chris Bovitz
National Operational Hydrologic Remote Sensing Center (NOHRSC)
National Weather Service, NOAA
1735 Lake Drive West, Chanhassen, MN 55317-8582 USA
Phone:  +1 952 368-2507
    :  +1 952 361-6610 x 2507
Fax:    +1 952 361-6634
E-mail: christopher (dot) bovitz (at) noaa (dot) gov
Web:    http://www.nohrsc.noaa.gov


--
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql

Reply via email to