>>>>> "AJ" == Adriaan Joubert <a.joubert@albourne.com> writes:
AJ> Hi,
AJ> I've been adding some bit functions to postgres and this works fine,
AJ> until I tried to construct a function that returns 8 bits as a string,
AJ> e.g. 4 as 00000100. I constructed the function with the help of the
AJ> programmer manual.
AJ> Now I have the following problem on Friday's snapshot: the first time I
AJ> select an integer as bitstring it works fine, but the second time I get
AJ> a segflt. So I'm evidently missing something somewhere. I'd appreciate
AJ> it if somebody could tell me what I'm doing wrong.
AJ> I'm getting (from the backend)
. . .
skip
. . .
AJ> text * bout (int a) {
AJ> int32 new_text_size = VARHDRSZ + sizeof(char)*8;
AJ> text *new_text = (text *) palloc(new_text_size);
AJ> int i;
AJ> for (i=0; i<8; i++)
AJ> VARDATA(new_text)[i] = (a>>(7-i))%2 ? '1' : '0';
AJ> return new_text;
AJ> }
You do not set length of data in *new_text. You must write about this:
---------------------------------
text * bout (int a) {
int32 new_text_size = VARHDRSZ + sizeof(char)*8;
text *new_text = (text *) palloc(new_text_size);
int i;
VARSIZE(new_text) = new_text_size; /* !!!!! */
for (i=0; i<8; i++)
VARDATA(new_text)[i] = (a>>(7-i))%2 ? '1' : '0';
return new_text;
}
---------------------------------
More: it'll be better if you change 'int' to 'int4' data type in your
nexts.
--
Anatoly K. Lasareff Email: tolik@icomm.ru
Senior programmer