Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id A3208636FAF for ; Thu, 30 Apr 2009 23:51:00 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 82965-05-5 for ; Thu, 30 Apr 2009 23:50:44 -0300 (ADT) X-Greylist: delayed 01:59:59.138079 by SQLgrey-1.7.6 Received: from mmp1.nems.noaa.gov (mmp1.nems.noaa.gov [140.90.121.156]) by mail.postgresql.org (Postfix) with ESMTP id B4B3563F258 for ; Thu, 30 Apr 2009 00:18:25 -0300 (ADT) Received: from [10.1.10.145] ([192.46.108.142]) by mmp1.nems.noaa.gov (Sun Java System Messaging Server 6.2-2.05 (built Apr 28 2005)) with ESMTPSA id <0KIV00EVOVALSQ80@mmp1.nems.noaa.gov> for pgsql-sql@postgresql.org; Wed, 29 Apr 2009 18:18:22 -0400 (EDT) Date: Wed, 29 Apr 2009 17:18:21 -0500 From: Chris Bovitz Subject: freeing memory after fetch all To: pgsql-sql@postgresql.org Message-id: <49F8D22D.20008@noaa.gov> Organization: NOAA/NWS/NOHRSC MIME-version: 1.0 Content-type: text/plain; charset=ISO-8859-1; format=flowed Content-transfer-encoding: 7BIT User-Agent: Thunderbird 2.0.0.21 (X11/20090302) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200904/146 X-Sequence-Number: 32492 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