[fpc-pascal] Static local variables available?
noreply at z505.com
noreply at z505.com
Mon Jul 24 04:37:34 CEST 2017
On 2017-07-21 07:16, Sven Barth via fpc-pascal wrote:
> Am 21.07.2017 10:44 schrieb "Santiago A." <svaa at ciberpiula.net>:
>>
>> El 20/07/2017 a las 15:50, Sven Barth via fpc-pascal escribió:
>>>
>>> Am 20.07.2017 13:01 schrieb "Bo Berglund" <bo.berglund at gmail.com>:
>>> >
>>> > On Thu, 20 Jul 2017 11:11:50 +0200, Maciej Izak
>>> > <hnb.code at gmail.com> wrote:
>>> >
>>> > >2017-07-20 11:03 GMT+02:00 Bo Berglund <bo.berglund at gmail.com>:
>>> > >
>>> > >> So since I don't really want to use a global, is it possible
> to
>>> > >> declare a local variable static in the sense that it retains
> its
>>> > >> values across calls to the procedure?
>>> > >> If so how is it done?
>>> > >>
>>> > >
>>> > >procedure foo;
>>> > >{$PUSH}
>>> > >const{$J+}
>>> > > s : string ='';
>>> > >{$POP}
>>> >
>>> > Thanks,
>>> > but it looks a bit involved, probably better to use an object
> field
>>> > variable instead only accessible from the internal methods.
>>>
>>> If you don't want to use $push/$pop then you can also simply enable
> $J+ for the whole unit. But this will also mean that global constants
> are writable.
>>
>> Well, I'm an old dog so I prefer old fashion ways. A variable that
> retains its value is, from memory point of view, a global variable. I
> mean, the memory is never freed, it's not freed when it goes out of
> scope. So, the problem is what to do to limit the visibility to the
> procedure.
>
> Writable constants *are* the old fashion way, cause this kind of usage
> stems from Turbo Pascal and allowed to restrict the visibility of
> symbols.
>
> Regards,
> Sven
Except really old fashioned might mean Modula
That's a neat idea to create a new module, and use it sort of like a
private object or static object just for the purpose of storing a global
variable in the private implementation section.
One disadvantage of modules (units) is that they add extra bytes to the
Exe, but is generally not an issue in desktop software. Previously when
people have asked for static C type things in fpc they were working with
embedded systems where every last byte counted and size of the program
was an issue. But, I don't know the use case in this episode..
More information about the fpc-pascal
mailing list