Server Programming in C: palloc() and pfree() - Mailing list pgsql-hackers
From | Rodrigo Hjort |
---|---|
Subject | Server Programming in C: palloc() and pfree() |
Date | |
Msg-id | 731083980602091016v6b894306y@mail.gmail.com Whole thread Raw |
Responses |
Re: Server Programming in C: palloc() and pfree()
Re: Server Programming in C: palloc() and pfree() |
List | pgsql-hackers |
I'm having some problems when using "pfree()" on functions in C.<br />Calling it on "psql" gives the exception below on bothversions of function "insert()" [1,2] if "pfree()" is enabled: <br /><br />server closed the connection unexpectedly<br/> This probably means the server terminated abnormally<br /> before or while processing therequest.<br />conexão com servidor foi perdida<br />(connection to the server was lost) <br /><br />The strange is thatit doesn't happen with the function "delstr()" [3], which has "pfree()".<br />What could am I doing wrong?<br /><br /><br/>// 1)<br />void insert(char *str, const int start, const char *piece) <br />{<br /> int lstr = strlen(str);<br /> int lnew = lstr + strlen(piece) + 1;<br /> char* temp = palloc(lnew);<br /> memset((void*) temp, 0, lnew);<br /><br/> /*<br /> FILE *debug;<br /> debug = fopen("/dev/xconsole", "w"); <br /> fprintf(debug, "insert('%s', %d,'%s')\n", str, start, piece);<br /> //fprintf(debug, "0) '%s'\n", temp);<br /> */<br /><br /> if (start <= lstr+ 1)<br /> {<br /> strncpy(temp, str, start - 1); <br /> strcat(temp, piece);<br /> char* ptr = str+ start - 1;<br /> strcat(temp, ptr);<br /> strcpy(str, temp);<br /> }<br />// pfree(temp); // <-- hereit doesn't work...<br /><br /> /*<br /> fprintf(debug, "-> '%s'\n", str); <br /> fflush(debug);<br /> fclose(debug);<br/> */<br />}<br /><br />// 2)<br />void insert(char *str, const int start, const char *piece)<br />{<br/> int i, j;<br /> char* temp = palloc(strlen(str) + strlen(piece) + 1);<br /><br /> if (start - 1 <= strlen(str))<br /> {<br /> for (i = 0; i < start - 1; i++)<br /> temp[i] = str[i];<br /><br /> for(j = i; j < strlen(piece) + i; j++)<br /> temp[j] = piece[j - i];<br /><br /> for (; i < strlen(str);i++, j++) <br /> temp[j] = str[i];<br /><br /> temp[j] = '\0';<br /> strcpy(str, temp);<br/> }<br />// pfree(temp); // doesn't work...<br />}<br /><br />// 3)<br />void delstr(char *str, const int start,const int size)<br />{<br /> int i, j; <br /> char* temp = palloc(strlen(str) - size + 1);<br /><br /> for (i= 0; (i < start - 1) && (i < strlen(str)); i++)<br /> temp[i] = str[i];<br /><br /> for (j = start+ size - 1; j < strlen(str); i++, j++) <br /> temp[i] = str[j];<br /><br /> temp[i] = '\0';<br /> strcpy(str,temp);<br /> pfree(temp); // <-- here it works!<br />}<br /><br /><br clear="all" />--<br /><br />Regards,<br/><br />Rodrigo Hjort<br />GTI - Projeto PostgreSQL<br /> CELEPAR - Cia de Informática do Paraná<br /><a href="http://www.pr.gov.br"target="_blank"> http://www.pr.gov.br</a><br />
pgsql-hackers by date: