[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] vec() & unpack() 5.0.6r1m



John_Peterson@byu.edu writes:
>> Why does the following script only produce the expected results the first ti
me
>> the script is run?
>> 
>> for ($i = 0;$i < 5;$i++) {
>>     vec($vector, $i, 1) = 1;
>>     print $i, '. Setbits are: ', unpack("%32b*", $vector), "\n";
>> }
>
>The relevent code from pp.c is:
>
>...
>    static char* bitcount = 0;
>...
>               if (!bitcount) {
>                   Newz(601, bitcount, 256, char);
>
>However, when a script ends, the CleanupPerl routine (in MPScript.c) calls:
>       free_pool_memory('PERL');
>
>which frees the memory allocated to the array.  The pointer bitcount then
>becomes a stale pointer to unallocated memory which is written over when the 
>next script runs.  Ouch.

Yes, this diagnosis is correct.

>One relatively easy way to fix this would be to declare bitcount as a static 
>array with the values already defined, like this:
>
>static char bitcount[256] =
>              { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,

That's what I'm going to do in the next release.

Matthias

-----
Matthias Neeracher <neeri@iis.ee.ethz.ch> http://err.ethz.ch/members/neeri.html
   "One fine day in my odd past..." -- Pixies, _Planet of Sound_