Array cache problem?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Ted
Reply | Threaded
Open this post in threaded view
|

Array cache problem?

Ted
Hello everybody,
I've been absent for a while, but hopefully you'll see me here again
more frequently. Happy new year!
Now my problem:
I've got this method that counts the number of occurrences in between a
defined range of bands. For example the bands are <5, <10, >10 and the
array of numbers is
#(2 6 8 9) then I expect an array back of #(1 3 0). I made a test for
this and guess what, the first time it works, but subsequent times it
adds the score onto the previous. So the first time it returns #(1 3
0), the second time it returns #(2 6 0) and so on.
I really can't see the logic of this other than some internal
stack/cache not being cleared. And the 'funny' thing is, if you step
through the method, it all works as expected, so I guess it's some kind
of optimisation.
The following link has a package with a Demo class and a DemoTest class
demonstrating the issue.
<http://www.bracht.nildram.co.uk/ConfusedStackDemo.pac>

Anybody any suggestion as to how to avoid this? Blair/Andy - is this a
bug?

Ted


Reply | Threaded
Open this post in threaded view
|

Re: Array cache problem?

Yar Hwee Boon-3
On 10 Jan 2005 05:07:47 -0800, Ted <[hidden email]> wrote:

> I really can't see the logic of this other than some internal
> stack/cache not being cleared. And the 'funny' thing is, if you step
> through the method, it all works as expected, so I guess it's some kind
> of optimisation.

The array stored in result, ie. #(0 0 0 0 0) is a "compile time" statement  
(not sure if the terminology is correct), so that array instance is stored  
together with the compiled method. Running the test again will just update  
the same array instance unless you recompile. If you change it to  
something like

     result := #(0 0 0 0 0) copy.

it should be ok. HTH.

--
Regards
HweeBoon
MotionObj


Reply | Threaded
Open this post in threaded view
|

Re: Array cache problem?

Chris Uppal-3
In reply to this post by Ted
Ted wrote:

> I really can't see the logic of this other than some internal
> stack/cache not being cleared. And the 'funny' thing is, if you step
> through the method, it all works as expected, so I guess it's some kind
> of optimisation.

>From your example package:

    result := #(0 0 0 0 0).

You are using an array literal to hold your result.  The actual array
corresponding to the literal is created /once/ (at compile-time) and stored
into the compiled method (try inspecting the method itself -- ctrl+I in the
method list in a CHB -- and you'll see it).  Each time you execute the method
it will further modify the contents of the array, and so the results will
change each time.  Changing that line to read:

    result := Array new: 5 withAll: 0.

should fix it.

    -- chris


Ted
Reply | Threaded
Open this post in threaded view
|

Re: Array cache problem?

Ted
Hi Chris and HweeBoon,
Thanks for the quick reply. This newsgroup is still Top!

Ted