Hi,
I posted a message yesterday about creating a custom datatype where I store an array of custom strucs. Well I finished
implementingit today, but I can not get it to work within postgres. I have compiled the C code in a seperate test
file(rippingout all the postgres related function calls), and gotten it to store my custom data type perfectly and then
spitit back out as a string. My problem is when I try to use it in postgres, the input argument to my output
function(thepointer 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
statementand result is not a null pointer. So what could be going on here? I am really stumped... Keep in mind if I
abstractthis to seperate file with no postgres refrences, then IT WORKS PERFECT. So somehow I have passed something
incorrectlyto 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
customdata type is defined as :
CREATE TYPE atomlocation (
internallength = VARIABLE,
input = atomlocation_in,
output = atomlocation_out
);