Board index » delphi » How to declare the static variables of C in Pascal?

How to declare the static variables of C in Pascal?

I am wondering whether there is something working like the C's static variable
for Pascal?  I don't like global variables sometime.  Any suggestion?

Thanks in advance.

Regards,
TJ Huang.

 

Re:How to declare the static variables of C in Pascal?


In article <41pbi4$...@ccnews.ncku.edu.tw>, she...@sparc16.cc.ncku.edu.tw (T.J. Prentice Huang) writes:

Quote
> I am wondering whether there is something working like the C's static variable
> for Pascal?  I don't like global variables sometime.  Any suggestion?

> Thanks in advance.

> Regards,
> TJ Huang.

You would define a constant within the procedure (or function) and this will
be allocated a permanent memory address (exactly the same as the instructions
used to allocate memory to a global variable), for example ...

Program StaticByteTestApplication;

Procedure MyProcedure;
Const
  StaticByte : Byte = 0;
Begin
  WriteLn('The value of StaticByte was ...... ', StaticByte);
  Inc(StaticByte);
  WriteLn('The value of StaticByte is now ... ', StaticByte);
End;

Begin
  MyProcedure;
  MyProcedure;
End.

The only problem with this style of 'variable' definition is that the variable
has a limited scope. I.E. It can only be accessed within the procedure (or
function) within which it is defined.

So what has actually happened ... ?

Ok, we've managed to combine both a Const and a Var together. Allowing us to
allocate memory on a permanent basis to a locally accessible variable, with the
added bonus of being able to initialise it at compile time; In this example
StaticByte would have the runtime value of 0 (zero), which can be modified as
required as long as it remains within the boundary of the type defintion.

Hope this is of some help.

Re:How to declare the static variables of C in Pascal?


In article <41pbi4$...@ccnews.ncku.edu.tw>, she...@sparc16.cc.ncku.edu.tw
says...

Quote

>I am wondering whether there is something working like the C's static
variable
>for Pascal?  I don't like global variables sometime.  Any suggestion?

>Thanks in advance.

>Regards,
>TJ Huang.

Try something like that :

Program Example ;

Var
     Static1    : Integer ;

{----------------------------------------}
Procedure DoSomething(X : Integer) ;
Var
    Y  : Integer absolute Static1 ;
Begin
    Y := Y + X ;
End
{----------------------------------------------}

Begin
   Static1 := 0 ;
   DoSomething(2) ;       {Y = 2}
   DoSomething(5) ;       {Y = 7}
End.

Re:How to declare the static variables of C in Pascal?


she...@sparc16.cc.ncku.edu.tw (T.J. Prentice Huang) wrote:

Quote
>I am wondering whether there is something working like the C's static variable
>for Pascal?  I don't like global variables sometime.  Any suggestion?
>Thanks in advance.
>Regards,
>TJ Huang.

Just to add to a previous posting, you can use the CONST trick to get
Global static variables--they are called Typed Constants in BP.

Program Foo;

CONST
  MyResult : INTEGER = 0;
...

This will create a global variable that is initialized to zero.  Be
warned though, that this data is allocated as long as the program is
running in the data segment, not the stack.  There are times when this
is useful, though.

You can even do this in the Interface section of Units (but it makes
the program much harder to maintain).

Mark

Other Threads