Major problem with custom data type

From: "Morgan Kita" <mkita(at)verseon(dot)com>
To: <pgsql-novice(at)postgresql(dot)org>
Subject: Major problem with custom data type
Date: 2005-04-02 03:45:35
Message-ID: 08B420FF5BF7BC42A064212C2EB768801C1093@neutron.verseon.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Hi,

I posted a message yesterday about creating a custom datatype where I store an array of custom strucs. Well I finished implementing it today, but I can not get it to work within postgres. I have compiled the C code in a seperate test file(ripping out all the postgres related function calls), and gotten it to store my custom data type perfectly and then spit it back out as a string. My problem is when I try to use it in postgres, the input argument to my output function(the pointer to the data) seems to be corrupted somehow.

In my input function I have this(after parsing the input string and setting up the data):
/*
The AtomLocation is the type of the struc to be stored in the array
temp_index is the number of structs to be stored and I add 4 for the word at the beginning that tells postgres the length.
point_strucs is a pointer to the beginning of the actual strucs.
temp_points is a local array that is temporarily holding the data that is internal to the strucs./*

result = (void *) palloc((sizeof(AtomLocation) * temp_index) + 4);
*((int *) result) = (sizeof(AtomLocation) * temp_index) + 4;
point_strucs = (AtomLocation *) (((char *) result) + 4);

for(i = 0; i < temp_index; i++){
point_strucs[i].index = temp_points[i].index;
point_strucs[i].x = temp_points[i].x;
point_strucs[i].y = temp_points[i].y;
point_strucs[i].z = temp_points[i].z;
}

PG_RETURN_POINTER(result);

My output function is obviously not getting passed something correctly. My first two lines are:

int* result = (int *) PG_GETARG_POINTER(0);
int length = *result - 4;

By some simple fprintf statements I can see that when I try to call *result - 4, that it crashes. I used an if statement and result is not a null pointer. So what could be going on here? I am really stumped... Keep in mind if I abstract this to seperate file with no postgres refrences, then IT WORKS PERFECT. So somehow I have passed something incorrectly to the PG database.

P.S I tried detoasting the value and all that did was cause it to fail on the first output function line of code. This custom data type is defined as :
CREATE TYPE atomlocation (
internallength = VARIABLE,
input = atomlocation_in,
output = atomlocation_out
);

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Tom Lane 2005-04-02 04:21:24 Re: Major problem with custom data type
Previous Message Michael Fuhr 2005-04-02 01:41:14 Re: how to ignore accents?