I was hoping someone here might help me as I'm working on the PECL extension AMFEXT.

I sincerely hope someone might take a moment to help me grok pointers. I have some source code that I'm trying to understand, namely the json.c file that underlies PHP's JSON extension.

When one calls the json_encode function within a PHP script, some magic happens wherein the zend engine parses a ZVAL object and a LONG, creates a char pointer, and some other vars, and then feeds them to a function, php_json_encode which, in turn, passes the zval function such as json_encode_array.

Now there are a lot of pointers (and macros) flying in this code sample which is confusing to me as my C/C++ is extremely rusty. I'm trying to understand a few things:
1) The call to zend_parse_parameters on line 565 passes the address-of value of parameter because we want the zend_parse_parameters function to change this value, right?
2) The first call to php_json_encode (on line 559) passes the var parameter (a zval*) directly to the function. Am I correct in understanding that parameter contains the memory address of a zval object?
3) The php_json_encode function receives the parameter as val, a pointer to a zval object. If this object is an array or object, then its address passed on to the json_encode_array function on line 475. Why use the & (address-of) operator here? Is this just sloppy and/or random use of pointers and & operators or is it necessary for some reason? Keep in mind the json_encode_array function does not need to alter the contents of val.

Any help would be much appreciated.

    No help from me: I never did like C and always got confused by pointers. 😉

      NogDog, your signature is so appropriate for your response. And I agree that pointers kinda suck (please forgive me Dennis Ritchie). And I would like to take this moment to thank divine providence for garbage collection and automatic memory management.

      However, I've got a lot of folks hankering for a newly reborn AMFEXT (the old one just doesn't work). Sadly, I must brave pointer hell. Maybe laserlight will chime in.

        sneakyimp wrote:

        1) The call to zend_parse_parameters on line 565 passes the address-of value of parameter because we want the zend_parse_parameters function to change this value, right?

        Yes.

        sneakyimp wrote:

        2) The first call to php_json_encode (on line 559) passes the var parameter (a zval*) directly to the function. Am I correct in understanding that parameter contains the memory address of a zval object?

        Line 559 is the start of a function definition, not a function call.

        sneakyimp wrote:

        3) The php_json_encode function receives the parameter as val, a pointer to a zval object. If this object is an array or object, then its address passed on to the json_encode_array function on line 475. Why use the & (address-of) operator here? Is this just sloppy and/or random use of pointers and & operators or is it necessary for some reason? Keep in mind the json_encode_array function does not need to alter the contents of val.

        It looks like a zval* parameter for json_encode_array would have sufficed. However, within json_encode_array, a zval** was needed to be passed to json_determine_array_type, so it certainly is not a random adding of another level of indirection.

          Thanks, laserlight! Merely mentioning you or Weedpacket's name seems to magically summon you like a genie or something. I noticed you had some 15,000 posts on another site which has a C programming forum so I was hoping you might chime in.

          laserlight;10990712 wrote:

          Line 559 is the start of a function definition, not a function call.

          Oops. I meant line 571
          The variableparameter contains the memory address of a zval object, correct?

          laserlight;10990712 wrote:

          It looks like a zval* parameter for json_encode_array would have sufficed. However, within json_encode_array, a zval** was needed to be passed to json_determine_array_type, so it certainly is not a random adding of another level of indirection.

          I see that, but the function json_determine_array_type just dereferences the pointer sent to it immediately. Am I right in thinking that there's no real need for using the address-of (&) operator on line 475 but that removing it would require us to make other changes?

          In my quest to get re-acquainted with pointers, I'm just wondering why this was done. Seems unecessary to me.

            sneakyimp wrote:

            Merely mentioning you or Weedpacket's name seems to magically summon you like a genie or something.

            starts humming a Christina Aguilera tune

            sneakyimp wrote:

            Oops. I meant line 571
            The variable parameter contains the memory address of a zval object, correct?

            Yes.

            sneakyimp wrote:

            I see that, but the function json_determine_array_type just dereferences the pointer sent to it immediately.

            That is true. Of course, there might be legacy reasons for that.

            sneakyimp wrote:

            Am I right in thinking that there's no real need for using the address-of (&) operator on line 475 but that removing it would require us to make other changes?

            Yes.

              Thanks for the responses. Thread complete.

                Write a Reply...