erealloc and the API
| From: | Johan Ekenberg | Date: | Fri, 04 May 2001 16:49:47 +0000 |
| Subject: | erealloc and the API | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-53875@lists.php.net to get a copy of this message | ||
I have a question regarding the API, something that bit me while I was
working with the php_imap code:
Basically I did this (pseudocode):
char init_value[10] = "foobar";
tmp_variable = emalloc(strlen(init_value) + 1);
strcpy(tmp_variable, init_value);
while(condition) {
erealloc(tmp_variable, strlen(tmp_variable) +
strlen(stuff_to_be_added) + 1);
sprintf(tmp_variable, "%s%s", tmp_variable, stuff_to_be_added);
}
other_variable = emalloc(strlen(tmp_variable) + 1);
strcpy(other_variable, tmp_variable);
efree(tmp_variable);
This produced some very weird results. At best I got messages about
leeking memory from tmp_variable, at worst there were segfaults in the
PHP memory manager, particularly in libc5 calling free() from
php_module_shutdown() if I recall correctly. Finally I ended up doing a
lot of copying to and from temporary variables without using erealloc().
But IMHO the code would be smaller and more consistent using erealloc().
Did I miss something obvious? Any references to existing API docs where
this is documented would be greatly appreciated.
Please cc replies to me, since I'm currently not subscribed to this
list.
Best regards,
/Johan Ekenberg