[fpc-pascal] Where is the best place to declare an array?

José Mejuto joshyfun at gmail.com
Thu May 6 20:58:42 CEST 2010

Hello FPC-Pascal,

Thursday, May 6, 2010, 7:12:46 PM, you wrote:

>>> If I remember this correctly, I've formerly ever read somewhere (in
>>> my Delphi days) that array should be declared globally (not inside a
>>> function or procedure) so that access to the array will be faster. Is
>>> this correct?
>> No.
WVB> That's a brief answer. How is what he is saying not true ?
WVB> A global variable can be allocated in the datasegment, meaning that the
WVB> compiler knows at comiletime what address to look for. If you allocate
WVB> things dynamically then you need to follow an extra pointer, so placing
WVB> an array in the datasegment would indeed speed up things afaik ? If not,
WVB> more than a blunt 'no' would be appreciated,

Static arrays.

Main difference is the scope. If you declare it in the function, the
array is not accesible outside while the global one is errrr... global
:) Both have not allocation time, the global is allocated at compile
time and the local one is allocated when calling the function
increasing the stack pointer, but as the stack pointer must be
increased also to perform the call, there is no penalty. Dynamic
arrays are different or arrays that needs an initialization, but
anyway using global ones you must initialize them to be reused.

Best regards,

More information about the fpc-pascal mailing list