RE: [PHP-DEV] when to duplicate string using add_index_string?
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? 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 :) [...] So what you're saying is - if duplicate is set to 0, you create a reference - if duplicate is set to 1, you increase the reference counter so that 'duplicate' means 'I am a duplicate', instead of what I first thought 'duplicate me' (which is why I sent a 1 first (duplicate me), and a 0 second). OK, that's clear now. Another question that's related to this: Am I right when I say: when I have created and initialized a zval in a function (MAKE_STD_ZVAL) I should also free it in this function (FREE_ZVAL), except when I add it directly to the hashtable (zend_hash_index_update), because then it is freed automatically? The following code-snippet from a function (dbx_query) actually works and doesn't crash or leak, based on this assumption // fill each row array with fieldvalues (indexed and assoc) row_count=0; result=1; while (result) { zval * rv_row; MAKE_STD_ZVAL(rv_row); result = switch_dbx_getrow(rv_row, rv_result_handle, DBX_RESULT_INDEX | DBX_RESULT_ASSOC, INTERNAL_FUNCTION_PARAM_PASSTHRU, dbx_module); if (result) { zend_hash_index_update(data-value.ht, row_count, (void *)(rv_row), sizeof(zval *), NULL); ++row_count; } else { FREE_ZVAL(rv_row); } } And yes, as you have guessed, I've started on creating a database abstraction module :-) It is called 'dbx' and currently already works for mysql... Since I want this Open Source'd (and preferably in the php-cvs-tree as well), how would I go about doing that (this will be my first open source code)? Thanks, Marc. -- 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]
RE: [PHP-DEV] when to duplicate string using add_index_string?
At 15:51 8/2/2001, Marc Boeren wrote: 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? 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 :) [...] So what you're saying is - if duplicate is set to 0, you create a reference - if duplicate is set to 1, you increase the reference counter so that 'duplicate' means 'I am a duplicate', instead of what I first thought 'duplicate me' (which is why I sent a 1 first (duplicate me), and a 0 second). No, it's not clear I guess :) 'duplicate' does mean 'duplicate me'. If duplicate is set to 0, then you tell the engine not to duplicate the string, but use it as-is. If it's set to 1, then you tell the engine to duplicate it, because this string is either static, or already referenced by other things in PHP. 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]
Re: [PHP-DEV] when to duplicate string using add_index_string?
On Thu, Feb 08, 2001 at 03:45:33PM +0200, Zeev Suraski wrote: 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 :) [...] So what you're saying is - if duplicate is set to 0, you create a reference - if duplicate is set to 1, you increase the reference counter so that 'duplicate' means 'I am a duplicate', instead of what I first thought 'duplicate me' (which is why I sent a 1 first (duplicate me), and a 0 second). No, it's not clear I guess :) 'duplicate' does mean 'duplicate me'. If duplicate is set to 0, then you tell the engine not to duplicate the string, but use it as-is. If it's set to 1, then you tell the engine to duplicate it, because this string is either static, or already referenced by other things in PHP. Perhaps 0 and 1 should be replaced by more verbose #define's (constants) to make this code clearer, then. -- Jon Parise ([EMAIL PROTECTED]) . Rochester Inst. of Technology http://www.csh.rit.edu/~jon/ : Computer Science House Member -- 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]