Re: Weird problems with C extension and bytea as input type

Поиск
Список
Период
Сортировка
Искать
От
Merlin Moncure
Тема
Re: Weird problems with C extension and bytea as input type
Дата
Msg-id
AANLkTinyQihT0BAeuoMdL+cO1zxOFZmvNDyiLFErpmGR@mail.gmail.com
Ответ на
Список
Дерево обсуждения
Weird problems with C extension and bytea as input type Adrian Schreyer <ams214@cam.ac.uk>
Re: Weird problems with C extension and bytea as input type Merlin Moncure <mmoncure@gmail.com>
Re: Weird problems with C extension and bytea as input type Adrian Schreyer <ams214@cam.ac.uk>
Re: Weird problems with C extension and bytea as input type Adrian Schreyer <ams214@cam.ac.uk>
Re: Weird problems with C extension and bytea as input type Merlin Moncure <mmoncure@gmail.com>
Re: Weird problems with C extension and bytea as input type David W Noon <dwnoon@ntlworld.com>
Re: Weird problems with C extension and bytea as input type Adrian Schreyer <ams214@cam.ac.uk>
Re: Weird problems with C extension and bytea as input type Tom Lane <tgl@sss.pgh.pa.us>
Re: Weird problems with C extension and bytea as input type dennis jenkins <dennis.jenkins.75@gmail.com>
Re: Weird problems with C extension and bytea as input type Merlin Moncure <mmoncure@gmail.com>
Re: Weird problems with C extension and bytea as input type Adrian Schreyer <ams214@cam.ac.uk>
Re: Weird problems with C extension and bytea as input type Adrian Schreyer <adrian@schreyer.me>
Re: Weird problems with C extension and bytea as input type Adrian Schreyer <adrian@schreyer.me>
On Wed, Mar 23, 2011 at 9:04 AM, dennis jenkins
 wrote:
> On Wed, Mar 23, 2011 at 5:08 AM, Adrian Schreyer  wrote:
>>
>> you are right, it returns a char *.
>>
>> The prototype:
>>
>> char *function(bytea *b);
>>
>> The actual C++ function looks roughly like this
>>
>> extern "C"
>> char *function(bytea *b)
>> {
>>   string ism;
>>   [...]
>>   return ism.c_str();
>> }
>>
>
>
> Don't do that.  You are returning a pointer to an unallocated buffer
> (previously held by a local variable).  c_str() is just a const
> pointer to a buffer held inside "ism".  When ism goes out of scope,
> that buffer if freed.
>
> Either return "std::string", or strdup() the string and have the
> caller free that.  (but use the postgresql alloc pool function to
> handle the strdup.  I don't recall that function's name off the top of
> my head).

that would be pstrdup, and it's the way to go (you don't have to
pfree).  who says C doesn't have garbage collection?

merlin
В списке pgsql-general по дате отправления
От: Merlin Moncure
Дата:
От: Tom Lane
Дата:
FAQ