Обсуждение: formatting the output of a function

Поиск
Список
Период
Сортировка

formatting the output of a function

От
"Islam Hegazy"
Дата:
Hi all
 
I created a function that returns a set of records. This function returns an integer and a float as the record fields. I have a problem in this function, it truncates the output. e.g. 1342 is displayed as 134, 456.46 is displayed as 456. In other words, it displays the first 3 digits only of a number, whether it is integer of float. I traced the function and the results are computed correctly. I use PostgreSQL 8.2.4. Following is a piece of my code.
 

int counter;

float  result;

//some code

char **output;

output = (char**)palloc(2*sizeof(char*)); //allocate space for two string pointer

output[0] = (char*)palloc(sizeof(int)); //allocate space for a string that accepts an integer

output[1] = (char*)palloc(sizeof(double)); //allocate space for a string that accepts an integer

snprintf(output[0], sizeof(int), "%d", counter);

snprintf(output[1], sizeof(float), "%.5f", result);

HeapTuple data;

Datum finalResult;

data = BuildTupleFromCStrings(attinmeta, output);

finalResult = HeapTupleGetDatum(data);

SRF_RETURN_NEXT(funcctx, finalResult);

---------------

Any idea what could be the cause of this error?

Regards

Islam Hegazy

Re: formatting the output of a function

От
Martijn van Oosterhout
Дата:
On Wed, Jun 06, 2007 at 01:45:43PM -0600, Islam Hegazy wrote:
> output[0] = (char*)palloc(sizeof(int)); //allocate space for a string that accepts an integer

No it doesn't, it allocates space for an integer

> output[1] = (char*)palloc(sizeof(double)); //allocate space for a string that accepts an integer

This allocates space for a double.
>
> snprintf(output[0], sizeof(int), "%d", counter);
> snprintf(output[1], sizeof(float), "%.5f", result);

And here you're using sizeof(float) which is even smaller.

Don't try to be smart with the sizes, just use 16 bytes for both and in
the snprintf use 16 also, then everything will work fine.

> HeapTuple data;
> Datum finalResult;
> data = BuildTupleFromCStrings(attinmeta, output);

Although I think you're going the long way round, you can build a tuple
from the integer wtihout making a string first...

Have a nice day,
--
Martijn van Oosterhout   <kleptog@svana.org>   http://svana.org/kleptog/
> From each according to his ability. To each according to his ability to litigate.

Вложения

Re: formatting the output of a function

От
Richard Huxton
Дата:
Islam Hegazy wrote:
> Hi all
>
> I created a function that returns a set of records. This function returns an integer and a float as the record
fields.I have a problem in this function, it truncates the output. e.g. 1342 is displayed as 134, 456.46 is displayed
as456. In other words, it displays the first 3 digits only of a number, whether it is integer of float. I traced the
functionand the results are computed correctly. I use PostgreSQL 8.2.4. Following is a piece of my code. 

> output[0] = (char*)palloc(sizeof(int)); //allocate space for a string that accepts an integer
> output[1] = (char*)palloc(sizeof(double)); //allocate space for a string that accepts an integer

I don't really do C any more, but I don't think these do what you want.
I think here you're allocating space for an integer/double, not for the
string representation of them (i.e. 4/8 bytes). I'm surprised you're not
just getting a crash.

> snprintf(output[0], sizeof(int), "%d", counter);
> snprintf(output[1], sizeof(float), "%.5f", result);

--
   Richard Huxton
   Archonet Ltd