Re: C function woes

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To:
Cc: Chris Hayner <hayner80(at)astro(dot)ocis(dot)temple(dot)edu>, PostgreSQL-General <pgsql-general(at)postgresql(dot)org>
Subject: Re: C function woes
Date: 2001-02-20 22:56:30
Message-ID: 6973.982709790@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I wrote:
> text *
> hello()
> {
> char data[] = "hello world";
> int32 new_text_size = VARHDRSZ + sizeof(data);
> text *new_text = (text *) palloc(new_text_size);
>
> VARSIZE(new_text) = new_text_size;
> memcpy(VARDATA(new_text), data, sizeof(data));
> return new_text;
> }

Drat. Replace sizeof(data) by strlen(data) both places, and
*then* it's right. You don't want the trailing null in the
TEXT object.

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Roderick A. Anderson 2001-02-20 23:02:33 Re: Re: Printing PostgreSQL reports
Previous Message Tom Lane 2001-02-20 22:52:23 Re: C function woes