Bill Allombert on Thu, 26 Jul 2012 17:25:37 +0200


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

Re: Global persistent variables (compiled code)


On Thu, Jul 26, 2012 at 05:03:20PM +0200, Manolo wrote:
> I need to fix some global variables and so I initialize their values
> with init_library()-like function.
> 
> So:
> 
> -------
> static GEN myvar1;
> static GEN myvar2;
> /*End of global vars*/
> 
> void
> init_mylib(void)	  /* void */
> {
> 
>   static long init_done = 0;
>   if (init_done) return;
> 
> *** staff related to my global vars ***
> 
>  init_done = 1;
> }
> ------------
> 
> Then, I import the library on a pari session (via "install" "run"-file
> and so on...). If I try to run a function:
> 
> ? mylib_myfunction()
> 
> it returns error, because I didn't initialize the global vars. So I must run:
> 
> ? init_mylib(); mylib_myfunction()
> 
> And it works. That's ok up to now.
> 
> But if try again
> 
> ? mylib_myfunction()
> 
> or even try again the same step with initialization,
> 
> ? init_mylib(); mylib_myfunction()
> 
> I receive an error, because global vars are not ok!!! I must leave the
> session an enter again to get my code working.
> 
> I guess that "normal" static variables like "init_done" are kept, but
> pari-variables don't.

Yes, normally PARI variables are allocated on the PARI stack, which is emptied
when the GP prompt is displayed. 

If you need global variables to be outside of the PARI stack, you can use gclone in C
(the clone function under GP2C).
See the test file gp2c/test/gp/initfunc.gp
There is also an unclone function to free such variables.

Cheers,
Bill.