Sunteți pe pagina 1din 2

There are pluses and minuses on any choice of variable type, the following might

help. Note part of this discussion is masked by specific proprietary compiler


optimizations, use of memory, registers, stack addressing, architecture..so any
discussion is incomplete without the compiler designer present.
So Globals are not all bad, and they are not all good.My experience of late is
with code size limitations, eg. limited FLASH size, and small machine RAM devices.
So Globals have advantages here, but risks as Bob and many have pointed out.

Global vs Static Variables


Global variables are variables defined outside of any function. Their scope starts at the point
where they are defined and lasts to the end of the file. They have external linkage, which means
that in other source files, the same name refers to the same location in memory.
Static global variables are private to the source file where they are defined and do not conflict
with other variables in other source files which would have the same name.

As a sidenote, both global and static variables have static initialization, which means that if you
don't set them to a value, they will be initialized to 0 (common variables) or NULL (pointers).
This is the only case in ANSI C where you can assume that the value is zero without initializing
it explicitly.

Static global variables: variables declared as static at the top level of a source file
(outside any function definitions) are only visible throughout that file ("file scope", also
known as "internal linkage").

Static local variables: variables declared as static inside a function are statically
allocated, thus keep their memory cell thoughout all program execution, while having the
same scope of visibility as automatic local variables, meaning remain local to the
function. Hence whatever values the function puts into its static local variables during
one call will still be present when the function is called again.

The meaning of the "static" keyword in C depends on whether or not it appears within a bracedelimited block. Refer to the following code snippet.
1.
2.

int a;
static int b;

3.
4.

int func(void) {

5.

int c;

6.

static int d;

7.

...

8.

Variables 'a' and 'b' are declared outside of any brace-delimited blocks, therefore they both
persist for the life of the program and are accessible from anywhere in the compilation unit.
In this case, the "static" keyword has the effect of making variable 'b' local to the compilation
unit. Variable 'a' can be accessed from any other compilation units that contain an extern
declaration for it.
Variables 'c' and 'd' are declared within a brace-delimited block, therefore they are only
accessible from within that block. In this case, the "static" keyword has the effect of making
variable 'd' persist for the life of the program. Variable 'c' only persists while execution is
within that brace-delimited block. There is no reason to expect variable 'c' to be stored at the
same location for successive executions of the brace-delimited block.

Performance Impact

http://stackoverflow.com/questions/3835857/in-c-does-using-static-variables-in-afunction-make-it-faster

S-ar putea să vă placă și