memory concept for static variables inside the blocks in a program
The static vars in a program exists in the memory for all the execution time while the static vars of a subprogram are created (by invocations to the subprogram) and destroyed (by termination of the subprogram) which is done by pushing of the subprogram's activation record onto and popping of it off the program's function call stack, but:
What about the static vars in blocks(I mean control structures or any {} block) inside the main program? They aren't accessible outside their blocks where they are defined, How is memory concept for them?
Are they exist in the memory in the whole program execution but aren't accessible outside their blocks or there are activation records also for every block other than subprograms?
Static variables in all cases are allocated once for the lifetime of the program. (I think by "subprogram" in your question you mean a C function.) Your question is specific to the programming language in use, so I'm going to assume C.
The ability of a code block to "see" (or not see) the static variable is separate, and is a fiction enforced by the compiler's lexical scoping rules.
Specifically in C, static
variables at global scope, function scope and block scope are all stored once per program for the entire life of the program. In the following example (at least) 3 words will be allocated when the program starts:
static int globalWord;
int aFunction(void) {
static int aFunctionPrivateStatic;
}
int main(void) {
while (1) {
static int whilePrivateStatic;
// ...
}
// ...
}
See http://en.wikipedia.org/wiki/Static_variable for a more thorough example.
链接地址: http://www.djcxy.com/p/84330.html上一篇: GCC与x64 sta
下一篇: 程序中块内静态变量的内存概念