Local-global variables

Eveyrone knows that Delphi has typed constants.

const
  C: Integer = 777;

So the data of the constants is stored in the data segment (IDATA executable section is used).

But what happens when you define typed constant in a function or procedure?

Answer: The same – you just limit the scope. So typed constants are initialized when loading the executable and only once – they are not set to the value each time when you enter the function. As well these consts data is not in a stack.

procedure Run;
{$J+}
const
  C: Integer = 0;
begin
  Inc (C);
  ...
end;

The procedure above on each call will have value of C equal to the number of times routine is called.

So you have local global variable 🙂

May the Pascal be with you!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s