c extension

Поиск
Список
Период
Сортировка
От Kjetil Haaland
Тема c extension
Дата
Msg-id 200411031029.17336.kjetil.haaland@student.uib.no
обсуждение исходный текст
Ответы Re: c extension  (Joe Conway <mail@joeconway.com>)
Список 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 по дате отправления:

Предыдущее
От: Jeff Eckermann
Дата:
Сообщение: Re: Importing Microsoft Sql Server 2000
Следующее
От: Raphael Bauduin
Дата:
Сообщение: building a row with a plpgsql function