[fpc-pascal] Converting old pascal written for Pascal/MT+ compiler

Tomas Hajny XHajT03 at hajny.biz
Tue Apr 4 09:01:40 CEST 2023


On 2023-04-04 08:16, Jacob Kroon via fpc-pascal wrote:


Hi Jacob,

  .
  .
> But I suspect I have a new problem: With the old Pascal/MT+ compiler
> it would appear that local variables declared in functions/procedures
> have a life-time that spans the whole program, like a "static"
> declared variable in C. With fpc, it looks like locally declared
> variables are automatic, put on the stack(?), and so they go out of
> existence once out of scope ?

Yes, local variables are declared on stack and their lifetime is indeed 
equivalent to the time of processing the particular function/procedure.


> The program depends on this feature in the old compiler. I did some
> googling and found that putting local variables in a "const" section
> instead of "var" would make them have a "whole-program" lifetime, but
> then I need to provide them with an initial value.
> 
> Do I have any other option besides changing from "var" to "const"
> everywhere, and provide initial values in all declarations ?

You don't need to change "var" to "const" - if you want to ensure the 
variables to persist in between the function/procedure runs, you need to 
move them to the global level, i.e. outside of the functions/procedures. 
It is not advisable as a general practice (to keep all variables 
globally), because then you might e.g. access the same variable from 
multiple functions by mistake, but it would probably solve your issue. 
Obviously, you might need to solve potential conflicts if you use the 
same variable names in multiple functions/procedures (e.g. by prepending 
the function/procedure name to the variable name or something like 
that).

Tomas


More information about the fpc-pascal mailing list