Re: Problemas al imprimir cadenas de caracteres

From: Juan Pablo Espino <jp(dot)espino(at)gmail(dot)com>
To: Alvaro Herrera <alvherre(at)dcc(dot)uchile(dot)cl>
Cc: Manuel Sugawara <masm(at)fciencias(dot)unam(dot)mx>, "pgsql-es-ayuda(at)postgresql(dot)org" <pgsql-es-ayuda(at)postgresql(dot)org>
Subject: Re: Problemas al imprimir cadenas de caracteres
Date: 2005-04-18 23:33:46
Message-ID: 3e7daec105041816335383446c@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-es-ayuda

Hola nuevamente, gracias por sus respuestas.

On 4/18/05, Alvaro Herrera <alvherre(at)dcc(dot)uchile(dot)cl> wrote:
> On Mon, Apr 18, 2005 at 04:31:52PM -0500, Manuel Sugawara wrote:
>
> Un comentario mas,
>
> > > new = t->vl_dat;
> >
> > Necesitas copiar char a char la cadena (aqui ya perdiste la referencia
> > a la memoria previamente allocada, therefore, memory leak!). Revisa
> > memcpy(3) o strcpy(3).
>
> No puedes usar t->vl_dat como si fuera un string C; en particular no
> puedes usar strcpy(), porque los tipos varlena no son terminados en \0.
> Tienes que usar memcpy(new, t->vl_dat, VARSIZE(t)) y despues poner el \0
> al final.

Bueno intente esto pero no me funciona

Datum
ej2(PG_FUNCTION_ARGS)
{
int32 x = PG_GETARG_INT32(0);
text *t = PG_GETARG_TEXT_P(1);
int32 z;
int32 new_size = VARSIZE(t) - VARHDRSZ;
char *new = (char *) palloc(new_size);
char *nulo = NULL;

memcpy(new, t->vl_dat, VARSIZE(t));
memcpy(new + VARSIZE(t) - VARHDRSZ, nulo, 1);
z = x*8;
elog(INFO, "Cadena: %s", new);
PG_RETURN_INT32(z);
}

El sistema se cuelga y se reinicia cuando llamo a la función, gracias
de antemano, saludos

Juan P. Espino

In response to

Responses

Browse pgsql-es-ayuda by date

  From Date Subject
Next Message Alvaro Herrera 2005-04-18 23:51:35 Re: Problemas al imprimir cadenas de caracteres
Previous Message Alvaro Herrera 2005-04-18 22:52:22 Re: Cache de funciones