Re: erealloc and the API
| From: | Zeev Suraski | Date: | Fri, 04 May 2001 17:06:42 +0000 |
| Subject: | Re: erealloc and the API | ||
| References: | 1 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-53884@lists.php.net to get a copy of this message | ||
erealloc() (like realloc()) returns a pointer to the realloc'd string. It may or may not be the same pointer you specifed in the first argument. So, your code should look like:
...
tmp_variable = erealloc(tmp_variable, ...);
Otherwise, you're going to have both a bug and if it doesn't crash you, a leak :)
Zeev
At 19:49 4/5/2001, Johan Ekenberg wrote:
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) {-- Zeev Suraski <zeev@zend.com> CTO & co-founder, Zend Technologies Ltd. http://www.zend.com/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 -- PHP Development Mailing List <http://www.php.net/> To unsubscribe, e-mail: php-dev-unsubscribe@lists.php.net For additional commands, e-mail: php-dev-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net