Re: [sqlite] sqlite3_free_table question

2006-06-29 Thread Jay Sprenkle

On 6/29/06, Richard Boyd <[EMAIL PROTECTED]> wrote:


I now check for NULL to be sure that I'm trying to free up a pointer that
actually points to something. Is there an easy way for me to ensure that the
memory has been successfully freed up? I understand this might be a basic C
question rather than a specific Sqlite question, so apologies for that...


If you're using malloc()/free() there is no indication returned when you call
free(). It's assumed to always work. What people typically do is set the
pointer to NULL if it's not pointing to a valid allocated block of memory.


[sqlite] sqlite3_free_table question

2006-06-29 Thread Richard Boyd

Hi again,

I now check for NULL to be sure that I'm trying to free up a pointer that
actually points to something. Is there an easy way for me to ensure that the
memory has been successfully freed up? I understand this might be a basic C
question rather than a specific Sqlite question, so apologies for that...

Thanks again,
Richard.

-Original Message-
From: Dennis Cote [mailto:[EMAIL PROTECTED] 
Sent: 28 June 2006 21:46
To: sqlite-users@sqlite.org
Subject: Re: [sqlite] sqlite3_free_table question

Richard Boyd wrote:
> Basically I have a structure I use to hold data results:
>
> //=
> typedef struct {
>   char   *sqlStatement; // Either the sqlstatement to be executed or the
> last statement that was executed
>   char   **results; // the result stored in an array
>   char   *err_msg;  // the error message
>   intnumrows;   // number of rows of results
>   intnumcols;   // the number of columns of data returned
> } RESULT_T;
> //===
>
> I pass this structure to a wrapper function that ultimately calls
> sqlite3_get_table and returns the results structure on tresults
>
> //===
> int execute_command (size_t   nbytes,  char *command,   RESULT_T
> *tresults)
> //===
>
> In the function that calls execute_command(), I want to free up the memory
> that was allocated by sqlite3_get_table().
>
> Do I just call the free function like this:
> sqlite3_free_table(tresults.results); (assuming that I created a variable
> called 'tresults' of type RESULT_T in the calling function).
>
> Also, how do I free up the error messages? What if there are no results
> returned by sqlite3_get_table, can I still attempt to free up the memory
> with 'sqlite3_free_table'
>
> I appreciate you reading down this far, and sorry if my code snips are
hard
> to follow!!
>
>   
Richard,

Your call to sqlite3_free_table is correct.

You free the error message by calling sqlite3_free(tresult.err_msg).

If either pointer returned by sqlite3_get_table() is NULL, then no 
memory was allocated, so there is no need to free it, however I believe 
it should be safe to call the free routines with a NULL pointer.

HTH
Dennis Cote



-- 
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.1.394 / Virus Database: 268.9.6/378 - Release Date: 28/06/2006




Re: [sqlite] sqlite3_free_table question

2006-06-29 Thread drh
Dennis Cote <[EMAIL PROTECTED]> wrote:
> Christian Smith wrote:
> > sqlite3_free, and the sqlite3GenericFree that implements it asserts 
> > that the pointer passed in is not NULL. So it is not safe to pass in a 
> > NULL pointer.
> >
> > It should be safe, if sqlite3_free and co are mimicking the behaviour 
> > of the libc free. The current CVS implementation should therefore 
> > check for NULL and do nothing in this case.
> >
> I agree, this should be fixed. You should file a bug report.
> 

2 days ago.  See http://www.sqlite.org/cvstrac/chngview?cn=3298
--
D. Richard Hipp   <[EMAIL PROTECTED]>



Re: [sqlite] sqlite3_free_table question

2006-06-29 Thread Dennis Cote

Christian Smith wrote:
sqlite3_free, and the sqlite3GenericFree that implements it asserts 
that the pointer passed in is not NULL. So it is not safe to pass in a 
NULL pointer.


It should be safe, if sqlite3_free and co are mimicking the behaviour 
of the libc free. The current CVS implementation should therefore 
check for NULL and do nothing in this case.



I agree, this should be fixed. You should file a bug report.

Dennis Cote


Re: [sqlite] sqlite3_free_table question

2006-06-29 Thread Christian Smith

Dennis Cote uttered:



Your call to sqlite3_free_table is correct.

You free the error message by calling sqlite3_free(tresult.err_msg).

If either pointer returned by sqlite3_get_table() is NULL, then no memory was 
allocated, so there is no need to free it, however I believe it should be 
safe to call the free routines with a NULL pointer.



From the discussion the other day, I looked at the implementation of 
sqlite3_free, and the sqlite3GenericFree that implements it asserts that 
the pointer passed in is not NULL. So it is not safe to pass in a NULL 
pointer.


It should be safe, if sqlite3_free and co are mimicking the behaviour of 
the libc free. The current CVS implementation should therefore check for 
NULL and do nothing in this case.





HTH
Dennis Cote




Christian

--
/"\
\ /ASCII RIBBON CAMPAIGN - AGAINST HTML MAIL
 X   - AGAINST MS ATTACHMENTS
/ \


RE: [sqlite] sqlite3_free_table question

2006-06-28 Thread Richard Boyd
Dennis,

Ok thanks for the quick response. I'll persevere with that then and get back
if I still have problems.

Thanks,
Richard. 





Re: [sqlite] sqlite3_free_table question

2006-06-28 Thread Dennis Cote

Richard Boyd wrote:

Basically I have a structure I use to hold data results:

//=
typedef struct {
  char   *sqlStatement; // Either the sqlstatement to be executed or the
last statement that was executed
  char   **results; // the result stored in an array
  char   *err_msg;  // the error message
  intnumrows;   // number of rows of results
  intnumcols;   // the number of columns of data returned
} RESULT_T;
//===

I pass this structure to a wrapper function that ultimately calls
sqlite3_get_table and returns the results structure on tresults

//===
int execute_command (size_t   nbytes,  char *command,   RESULT_T
*tresults)
//===

In the function that calls execute_command(), I want to free up the memory
that was allocated by sqlite3_get_table().

Do I just call the free function like this:
sqlite3_free_table(tresults.results); (assuming that I created a variable
called ‘tresults’ of type RESULT_T in the calling function).

Also, how do I free up the error messages? What if there are no results
returned by sqlite3_get_table, can I still attempt to free up the memory
with ‘sqlite3_free_table’

I appreciate you reading down this far, and sorry if my code snips are hard
to follow!!

  

Richard,

Your call to sqlite3_free_table is correct.

You free the error message by calling sqlite3_free(tresult.err_msg).

If either pointer returned by sqlite3_get_table() is NULL, then no 
memory was allocated, so there is no need to free it, however I believe 
it should be safe to call the free routines with a NULL pointer.


HTH
Dennis Cote



[sqlite] sqlite3_free_table question

2006-06-28 Thread Richard Boyd
Hi all,

Firstly, forgive me for a slightly naïve question; I don’t normally code in
C and I’m having problems freeing up memory.

Basically I have a structure I use to hold data results:

//=
typedef struct {
  char   *sqlStatement; // Either the sqlstatement to be executed or the
last statement that was executed
  char   **results; // the result stored in an array
  char   *err_msg;  // the error message
  intnumrows;   // number of rows of results
  intnumcols;   // the number of columns of data returned
} RESULT_T;
//===

I pass this structure to a wrapper function that ultimately calls
sqlite3_get_table and returns the results structure on tresults

//===
int execute_command (size_t   nbytes,  char *command,   RESULT_T
*tresults)
//===

In the function that calls execute_command(), I want to free up the memory
that was allocated by sqlite3_get_table().

Do I just call the free function like this:
sqlite3_free_table(tresults.results); (assuming that I created a variable
called ‘tresults’ of type RESULT_T in the calling function).

I’ve tried quite a few permutations and am never quite sure if I’ve actually
freed up the memory. Is there a fool proof way to check?

Also, how do I free up the error messages? What if there are no results
returned by sqlite3_get_table, can I still attempt to free up the memory
with ‘sqlite3_free_table’

I appreciate you reading down this far, and sorry if my code snips are hard
to follow!!

TIA
Richard.