Skip site navigation (1) Skip section navigation (2)

Peripheral Links

Header And Logo

PostgreSQL
| The world's most advanced open source database.

Site Navigation

Search archives
  Advanced Search

freeing memory after fetch all


  • From: Chris Bovitz <Christopher(dot)Bovitz(at)noaa(dot)gov>
  • To: pgsql-sql(at)postgresql(dot)org
  • Subject: freeing memory after fetch all
  • Date: Wed, 29 Apr 2009 17:18:21 -0500
  • Message-id: <49F8D22D.20008@noaa.gov> <text/plain>

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




Home | Main Index | Thread Index

Privacy Policy | About PostgreSQL
Copyright © 1996 – 2012 PostgreSQL Global Development Group