Re: libpq-fe: PQgetvalue() ?

Lists: pgsql-sql
From: sad <sad(at)bankir(dot)ru>
To: pgsql-sql(at)postgresql(dot)org
Subject: libpq-fe: PQgetvalue() ?
Date: 2004-10-14 12:34:22
Message-ID: 200410141634.22769.sad@bankir.ru
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

hi

does PQgetvalue() allocate memory rof its result, it returns ?
the answer will help me in problem:
should i free some cstring_variable if
{ cstring_variable=PQgetvalue(pgresult_variable,0,0); }
and could i PQclear(pgresult_varible) while cstring_varible is in use.

thnx


From: ljb <ljb220(at)mindspring(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: libpq-fe: PQgetvalue() ?
Date: 2004-10-15 00:51:32
Message-ID: ckn6uj$g28$2@news.hub.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

sad(at)bankir(dot)ru wrote:
> hi
>
> does PQgetvalue() allocate memory rof its result, it returns ?
> the answer will help me in problem:
> should i free some cstring_variable if
> { cstring_variable=PQgetvalue(pgresult_variable,0,0); }
> and could i PQclear(pgresult_varible) while cstring_varible is in use.

From the documentation:
The pointer returned by PQgetvalue points to storage that is part of
the PGresult structure. One should not modify the data it points to, and
one must explicitly copy the data into other storage if it is to be used
past the lifetime of the PGresult structure itself.

So no, you must not free your copy of the pointer returned by PQgetvalue,
and no, once you PQclear the result your pointer is not valid. You should


From: Christoph Haller <ch(at)rodos(dot)fzk(dot)de>
To: sad <sad(at)bankir(dot)ru>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: libpq-fe: PQgetvalue() ?
Date: 2004-10-15 08:57:18
Message-ID: 416F90ED.A65CB3B1@rodos.fzk.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

AFAIK it does allocate memory.
You cannot PQclear(pgresult_varible) while cstring_varible is in use.
You do not need to free cstring_variable, PQclear(pgresult_varible) will do.
I personally prefer to allocate local memory, "strcpy" PQgetvalue,
and then PQclear. But that's a matter of taste, I suppose.

Regards, Christoph

sad wrote:

> hi
>
> does PQgetvalue() allocate memory rof its result, it returns ?
> the answer will help me in problem:
> should i free some cstring_variable if
> { cstring_variable=PQgetvalue(pgresult_variable,0,0); }
> and could i PQclear(pgresult_varible) while cstring_varible is in use.
>
> thnx
>


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Christoph Haller <ch(at)rodos(dot)fzk(dot)de>
Cc: sad <sad(at)bankir(dot)ru>, pgsql-sql(at)postgresql(dot)org
Subject: Re: libpq-fe: PQgetvalue() ?
Date: 2004-10-15 14:21:29
Message-ID: 8899.1097850089@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-sql

Christoph Haller <ch(at)rodos(dot)fzk(dot)de> writes:
> You cannot PQclear(pgresult_varible) while cstring_varible is in use.
> You do not need to free cstring_variable, PQclear(pgresult_varible) will do.
> I personally prefer to allocate local memory, "strcpy" PQgetvalue,
> and then PQclear. But that's a matter of taste, I suppose.

That is surely overkill.

PQgetvalue doesn't allocate new memory for its result. The docs are
reasonably clear, I thought:

The pointer returned by PQgetvalue points to storage that is part
of the PGresult structure. One should not modify the data it points
to, and one must explicitly copy the data into other storage if it
is to be used past the lifetime of the PGresult structure itself.

regards, tom lane