At 14:20 8/2/2001, Marc Boeren wrote:

>Hi!
>
>I'm using add_index_string and add_assoc_string to build an array, and these
>functions expect a 'duplicate' argument. When should I set this to 1 and
>when to 0?
>Right now I'm setting it to 1 on the first call (add_index_string) and to
>zero for the second (add_assoc_string).
>Does it matter if it is also set to 1 for the second call?

First of all, it definitely matters.  Sending the right value is extremely 
important.  If you send 1 instead of 0, then you would get a memory 
leak.  If you send a 0 instead of 1, you'll most likely crash.  So you 
really must send the right values each time :)

Generally, the only time when you should set duplicate to 0, is if you're 
passing a string that you allocated (using emalloc(), or a function that 
returns an emalloc()'d block), and that is not referenced by any other 
structure in PHP.

Examples:
- In your example, if you allocated the string you're passing, then you 
should be first sending a 0 (don't duplicate, use this string as-is), and 
then 1 (2nd time around you must duplicate it, because now it's already 
referenced by PHP).  Sending 1 and then 0 will also work, but it's 
conceptually wrong.
- If you're passing a string that is taken from one of the arguments you 
received, you must set duplicate to 1;  This string is already referenced 
by PHP in the argument that was passed - and PHP will try to free it as 
soon as you return from the function.  If you don't set duplicate to 1, 
then as soon as PHP frees it, your return value will be corrupted.

Zeev


-- 
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