2.1 Static

Variables can be allocated statically. Any storage that is allocated statically has a lifespan of the execution of a program. This means such variables start to exist as soon as a program is loaded, and they only cease to exist when a program exits.

Do not confuse the scope of a variable with its lifespan. In other words, even when a static variable is not in view, it continues to retain its state.

One major disadvantage of static variables is that the size allocated cannot change as a program executes. If an array has a size of $n$ integers, it can only have $n$ elements throughout the execution of a program. The size of a static array cannot grow nor shrink.

Memory can be allocated statically, as follows:

struct X pool[256];



Copyright © 2006-08-28 by Tak Auyeung