Re: CodeGen_PECL int pass-by-reference

From: Date: Thu, 13 Sep 2007 07:51:48 +0000
Subject: Re: CodeGen_PECL int pass-by-reference
References: 1 2 3  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-27918@lists.php.net to get a copy of this message
Le 13 sept. 07 à 05:12, Matthew Williamson a écrit :
On Sep 12, 2007, at 7:15 PM, Justin Patrin wrote:
Are you trying to get back an int or a pointer to an int? In C passing an int by reference means passing a pointer to an int (int * var). If the function is allocating an int and returning the pointer to it for you to use then it would be an int ** var.
No, pretty sure what I need back is a value--I'm not sure what I'd do with the pointer if it got returned to PHP anyway. nc_open, for example, takes three parameters. The last parameter is an int, which the nc_open function modifies, and sets to be what is basically a filehandle number, which is the handle that is used to refer to the file in later function calls. The function's /return value/ is an error code (or zero for no error), so it uses that third parameter to pass back the filehandle number. I did figure out part of my problem: the compiler warnings were there because when CodeGen_PECL generates the C code for the extension, any "int" types from the prototype are actually represented as "long"s, so I just had to do some type coercion in the <code> block to get the warning to go away. But I still have the fundamental problem that the variable I pass in the third parameter to nc_open doesn't get modified. E.g.: <?php $intHandle = 42; nc_open('/path/to/file.nc', NC_NOWRITE, $intHandle); // or &$intHandle ... neither works echo $intHandle; ?> outputs "42". I put a printf statement in the <code> block to output the value that the C code sees (after the wrapped C call to nc_open), and it was "3", for example. But the changed variable doesn't get sent back in the third parameter.
You should ask your questions on the PECL list since they are not related to CodeGen_PECL. For such functions, I'd recommend writing a wrapper function, which means you'd have to change the API a bit. In this case, I think the function should return a resource, not an int. In fact, the nc_open() C function should be used inside another function and which in turn would return a resource. IIRC, that's how the sqlite wrapper for PDO does, for example. I think pretty much every PHP extension does so. You might want to have a look at other extensions and how they open files and return a PHP resource. -- Bertrand Mansion Mamasam Work : http://www.mamasam.com Blog : http://golgote.freeflux.net

« previous php.pear.general (#27918) next »