Zeev,

thanks, that made all the difference!

I got a suggestion from another PHP-developer that mixing calls to
emalloc() and strcpy() might be a problem. Is this so?

/Johan

> 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) {
> >       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: [EMAIL PROTECTED]
> >For additional commands, e-mail: [EMAIL PROTECTED]
> >To contact the list administrators, e-mail:
> [EMAIL PROTECTED]
>
> --
> Zeev Suraski <[EMAIL PROTECTED]>
> CTO &  co-founder, Zend Technologies Ltd. http://www.zend.com/
>


-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to