Re: Arrays and Pointers



thehuby wrote:

> I have come across an issue using arrays ond objects; I am using an
> array to keep track of 'n' number of object(The code snippet below is
> from my initial testing)
>
> I first declare the object ($nr = new news_resource();), then call a
> function of the object ($nr->params_init(); ). This is the put into
> the first element of the db, I then call the function again and put the
> object in the next element array.
>
> However when I print it all out at the end, all the elements of the
> array store the object with the same values...

Oops, I didn't read it close enough...

> if I redeclare the object ($nr = new news_resource(); ) again, before
> calling the funciton then it gets added in fine.
>
> I'm sure its a typical behaviour and is to do with pointers - but I
> don't understand why reassiging the object ($obj = new my_class() )
> before callings its funciton should break the link to the original
> object (and maintain its state).
>
> In my mind it should either always put a copy in the array or else
> always be a pointer.
>
> Can anyone explain why PHP works this way?
>
> Code snippet below:
>
> $nr = new news_resource();

Assume:
$nr == 0xffff6224

> $nr->params_init( 0, "www.num1.com", "Link to num1", "This links to
> number 1", 0, 0 );
> $resources[] = $nr;

$resources[0] == 0xffff6224

> $nr = new news_resource(); //why does this mean that the code works
> and the array keeps a copy of the object instead of a pointer?

If you do the above line:
$nr == 0xffff65a4

Otherwise:
$nr == 0xffff6224

> $nr->params_init( 0, "www.num2.com", "Link to num2", "This links to
> number 2", 0, 0 );
> $resources[] = $nr;

If you reset $nr with new:
$nr == 0xffff6c40

else:
$resources[1] == 0xffff6224

> $nr = new news_resource();
> $nr->params_init( 0, "www.num3.com", "Link to num3", "This links to
> number 3", 0, 0 );
> $resources[] = $nr;

If you reset $nr with new:
$nr == 0xffff7cc2

else:
$resources[2] == 0xffff6224

So now you have 2 possible outputs:

not resetting $nr each time:
array(
0xffff6224
0xffff6224
0xffff6224
0xffff6224
);

Resetting $nr using "new" each time:
array(
0xffff6224
0xffff65a4
0xffff6c40
0xffff7cc2
);

Look at the above numbers assuming they are memory addresses... do you
see why it behaves that way?

--
Justin Koivisto, ZCE - justin@xxxxxxxxx
http://koivi.com
.



Relevant Pages

  • Re: char **argv & char *argv[]
    ... "pointer to pointer to char". ... >> pointer)) pointing to the first element of an array. ... so we have to start adding more context. ... type "pointer to char", rather than "array MISSING_SIZE of char". ...
    (comp.lang.c)
  • Re: why cannot assign to function call
    ... hypothetical C-like languages, ... sizeof business would still indicate that a pointer was being passed. ... talk about variables of an array type. ... the earlier version of the standard didn't have numbered ...
    (comp.lang.python)
  • Re: multi dimensional arrays as one dimension array
    ... please - where does the standard say that such a conversion ... Pointer conversion yields a pointer to the same object as ... exist only where there are array declarations. ...
    (comp.lang.c)
  • Re: Evaluating unary *
    ... 'arr' exists, ... value can be used with the same syntax as would be used to access a 2D array of the kind you're referring to, but that 2D array is just a different way of looking as the same object that was already created by the definition of 'arr'. ... to me, it makes sense to return a pointer to the first value of an array, but to return the address of the pointer to the first value of an array, is not directly possible as such. ... lea eax, ...
    (comp.std.c)
  • Re: App Caused Pocket PC to Soft Reset on its Own?
    ... I don't know of any specific error that is likely to cause a reset. ... problem code could have existed without showing this symptom. ... Check each pointer before you use it. ... Check array bounds. ...
    (microsoft.public.pocketpc.developer)