pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Chris Bovitz <Christopher.Bovitz@noaa.gov>
To: pgsql-sql@postgresql.org
Subject: freeing memory after fetch all
Date: Wed, 29 Apr 2009 17:18:21 -0500
Message-ID: <49F8D22D.20008@noaa.gov> (raw)

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




Message-ID: <49F8D22D.20008@noaa.gov>
Permalink:  ../49F8D22D.20008@noaa.gov/
Also on:    postgresql.org/message-id/49F8D22D.20008@noaa.gov

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: Christopher.Bovitz@noaa.gov
  Subject: Re: freeing memory after fetch all
  In-Reply-To: <49F8D22D.20008@noaa.gov>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox