c extension - Mailing list pgsql-novice

From Kjetil Haaland
Subject c extension
Date
Msg-id 200411031029.17336.kjetil.haaland@student.uib.no
Whole thread Raw
Responses Re: c extension
List 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

pgsql-novice by date:

Previous
From: Jeff Eckermann
Date:
Subject: Re: Importing Microsoft Sql Server 2000
Next
From: Raphael Bauduin
Date:
Subject: building a row with a plpgsql function