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

Jonas Maebe jonas.maebe at elis.ugent.be
Thu May 6 20:44:48 CEST 2010


On 06 May 2010, at 18:12, Werner Van Belle wrote:

> Jonas Maebe wrote:
>> 
>> On 06 May 2010, at 16:43, Bihar Anwar 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.
> That's a brief answer. How is what he is saying not true ?

I don't have the faintest idea why it would be true. This is a bit like asking someone to explain "how is the statement that bytes are faster than chars not true?"

> A global variable can be allocated in the datasegment, meaning that the
> compiler knows at comiletime what address to look for. If you allocate
> things dynamically then you need to follow an extra pointer,

Local variables (including arrays) are allocated on the stack and are accessed without any kind of indirection (except for dynamic arrays, but the same goes for globally defined dynamic arrays). In fact, it's accesses to global variables that may incur an extra indirection (in case position-independent code is used).

> so placing
> an array in the datasegment would indeed speed up things afaik ? If not,
> more than a blunt 'no' would be appreciated,

I don't know how to explain this briefly. I suggest you to simply try it out by compiling with -al, then you can see what kind of code the compiler generates for the different kind of variables.


Jonas


More information about the fpc-pascal mailing list