Memory Allocation Error

Started by zhugeover 18 years ago3 messagesbugs
Jump to latest
#1zhuge
zhuge@Rinaix.cn

I wrote and compiled and linked a C function, add_float, which returns
float in C like following:

PG_FUNCTION_INFO_V1(add_float);

Datum
add_float(PG_FUNCTION_ARGS)
{
float arg = PG_GETARG_FLOAT8(0);

PG_RETURN_FLOAT8(arg + 10);
}

After having loaded it into database, executed the SQL command, "select
ad_float(2.90)", but one error occured. The error message is "Error:
invalid memory alloc request size 4294967290" .

However I changed the above function as following:

PG_FUNCTION_INFO_V1(add_integer);
Datum
add_integer(PG_FUNCTION_ARGS)
{
int32 arg = PG_GETARG_INT32(0);

PG_RETURN_INT32(arg + 10);
}

and executed the SQL command, "select add_integer(5)", and the result is:
add_integer
----------
15

In another word, the function can NOT return float BUT return integer.
Why? Please help me.

Thank you very much.

#2Bruce Momjian
bruce@momjian.us
In reply to: zhuge (#1)
Re: Memory Allocation Error

"zhuge xiao" <zhuge@Rinaix.cn> writes:

I wrote and compiled and linked a C function, add_float, which returns
float in C like following:

PG_FUNCTION_INFO_V1(add_float);

Datum
add_float(PG_FUNCTION_ARGS)
{
float arg = PG_GETARG_FLOAT8(0);

PG_RETURN_FLOAT8(arg + 10);
}

After having loaded it into database, executed the SQL command, "select
ad_float(2.90)", but one error occured. The error message is "Error:
invalid memory alloc request size 4294967290" .

float8 and float aren't the same thing. "float" is a C data type which is not
wide enough to hold a float8. The first line actually works by shortening it
but the return doesn't work because it returns a pointer to the float and
claims it's a pointer to the float8.

--
Gregory Stark
EnterpriseDB http://www.enterprisedb.com

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Bruce Momjian (#2)
Re: Memory Allocation Error

Gregory Stark <stark@enterprisedb.com> writes:

"zhuge xiao" <zhuge@Rinaix.cn> writes:

I wrote and compiled and linked a C function, add_float, which returns
float in C like following:

float8 and float aren't the same thing. "float" is a C data type which is not
wide enough to hold a float8. The first line actually works by shortening it
but the return doesn't work because it returns a pointer to the float and
claims it's a pointer to the float8.

I don't think that's the problem, because PG_RETURN_FLOAT8 doesn't
actually do that. More likely is confusion between float4 and
float8 in the SQL declaration of the function.

regards, tom lane