I'm having some problems when using "pfree()" on functions in C.
Calling it on "psql" gives the exception below on both versions of function "insert()" [1,2] if "pfree()" is enabled:
server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
conexão com servidor foi perdida
(connection to the server was lost)
The strange is that it doesn't happen with the function "delstr()" [3], which has "pfree()".
What could am I doing wrong?
// 1)
void insert(char *str, const int start, const char *piece)
{
int lstr = strlen(str);
int lnew = lstr + strlen(piece) + 1;
char* temp = palloc(lnew);
memset((void*) temp, 0, lnew);
/*
FILE *debug;
debug = fopen("/dev/xconsole", "w");
fprintf(debug, "insert('%s', %d, '%s')\n", str, start, piece);
//fprintf(debug, "0) '%s'\n", temp);
*/
if (start <= lstr + 1)
{
strncpy(temp, str, start - 1);
strcat(temp, piece);
char* ptr = str + start - 1;
strcat(temp, ptr);
strcpy(str, temp);
}
// pfree(temp); // <-- here it doesn't work...
/*
fprintf(debug, "-> '%s'\n", str);
fflush(debug);
fclose(debug);
*/
}
// 2)
void insert(char *str, const int start, const char *piece)
{
int i, j;
char* temp = palloc(strlen(str) + strlen(piece) + 1);
if (start - 1 <= strlen(str))
{
for (i = 0; i < start - 1; i++)
temp[i] = str[i];
for (j = i; j < strlen(piece) + i; j++)
temp[j] = piece[j - i];
for (; i < strlen(str); i++, j++)
temp[j] = str[i];
temp[j] = '\0';
strcpy(str, temp);
}
// pfree(temp); // doesn't work...
}
// 3)
void delstr(char *str, const int start, const int size)
{
int i, j;
char* temp = palloc(strlen(str) - size + 1);
for (i = 0; (i < start - 1) && (i < strlen(str)); i++)
temp[i] = str[i];
for (j = start + size - 1; j < strlen(str); i++, j++)
temp[i] = str[j];
temp[i] = '\0';
strcpy(str, temp);
pfree(temp); // <-- here it works!
}
--
Regards,
Rodrigo Hjort
GTI - Projeto PostgreSQL
CELEPAR - Cia de Informática do Paraná
http://www.pr.gov.br
- [HACKERS] Server Programming in C: palloc() and pfr... Rodrigo Hjort
- Re: [HACKERS] Server Programming in C: palloc(... Martijn van Oosterhout
- Re: [HACKERS] Server Programming in C: palloc(... Tom Lane