c extension

From: Kjetil Haaland <kjetil(dot)haaland(at)student(dot)uib(dot)no>
To: pgsql-novice(at)postgresql(dot)org
Subject: c extension
Date: 2004-11-03 09:29:17
Message-ID: 200411031029.17336.kjetil.haaland@student.uib.no
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Hello

I am writing a c extension function to my postgres database and in this i am
trying to allocate memory space for a table of char. When i try to call a
function that returns a value from this table postgres crashes. It works fine
running it as c code. I have added part of the code and hope that someone can
help me.

char *scoreChar;

void readScoreMatrix(char *filename) {
scoreChar = (char*)palloc(20*sizeof(char));
if(scoreChar == NULL) {
printf("\n failed to allocate memory for scoreChar");
ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR),
errmsg("failed to allocate memory for scoreChar")));

then i fill the table with 20 characters.

PG_FUNCTION_INFO_V1(setscorematrix);

void setscorematrix(PG_FUNCTION_ARGS) {
void *fileName = PG_GETARG_POINTER(0);
char *file;
file = DatumGetCString((char *)DatumGetPointer(fileName));
readScoreMatrix(file);
}
Datum getscorechar(PG_FUNCTION_ARGS) {
int32 i = PG_GETARG_INT32(0);
char c = scoreChar[i];
//ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR),
// errmsg("test:%c", c)));
PG_RETURN_CHAR(c);
}
CREATE FUNCTION getscorechar(integer) returns char
AS '/home/funcs/test'
LANGUAGE C STRICT;

The reading of the scorechar works fine, returns no error. And if i write out
the value from the scoreChar as an error i get the correct character.

thanks for any help
-Kjetil

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Lars Eckberg 2004-11-03 11:42:32 function to update different tables
Previous Message stig erikson 2004-11-02 20:38:45 Re: Importing Microsoft Sql Server 2000