Scope vs life of variable in C
Could someone exactly explain the开发者_开发技巧 concept of scope and life of variable in C. Is it different in C++? I'm confused between scope and life.
"Scope" of a variable is a region of source code, where you can refer to that variable.
"Lifetime" is how long it exists during the program execution.
By default the lifetime of a local variable is the same as its scope:
void foo()
{
int x = 123;
cout << x << endl;
x += 1;
}
int main(){ foo(); foo(); foo(); }
Here, each time foo
is called a new x
is created (space is reserved for it on the stack), and when the execution leaves the block where x
was declared, x
is destroyed (which for int
just means that the space that was reserved, now is freed for reuse).
In contrast:
void foo()
{
static int x = 123;
cout << x << endl;
x += 1;
}
int main(){ foo(); foo(); foo(); }
Here, since x
is declared static
, space is reserved for x
before the program execution even begins. x
has a fixed location in memory, it's a static variable. And C++ has special rules about the initialization of such a variable: it happens the first time the execution passes through the declaration.
Thus, in the first call of foo
this x
is initialized, the output statement displays 123, and the increment increases the value by 1. In the next call the initialization is skipped (it has already been performed), the value 124 is output, and the value is incremented again. So on.
The lifetime of this x
is from start of execution to end of execution.
Scope is the region where the variable is accessible.
Life time is the time span during which an object remains valid.
An simple example:
#include <iostream.h>
void doSomething()
{
x = 5; //Error! Not Accessible
}
int main()
{
int x = 4;
std::cout<< x << endl;
{
int x = 2;
cout << x << endl;
}
doSomething();
std::cout<< x << endl;
return 0;
}
The above gives the output:
4
2
4
In above program,
lifetime of variable x = 4
is throughout the main, i.e: It remains alive throughout the execution of the main, Also it is accessible within the main, that is its scope. Note that it is not accessible in the function because it is beyond the scope of the variable x
.
while scope and lifetime of variable x = 2
is within the enclsing braces{
}
inside the main.
The scope of a variable is determined at compilation time. It is the region in the program where the same object that is defined through the definition is accessible through that identifier.
The lifetime of an object is a feature that is defined at runtime, through the flow of execution. An object can be accessed through a pointer even if the variable through which it was defined is not in scope.
void f(char *a) {
*a = 'f';
}
void g(void) {
char aChar = ' ';
f(&aChar);
}
Here the scope of variable aChar
(the identifier) is the body of g
. During the execution of g
the lifetime of the object expands to the execution of f
. Using the identifier aChar
inside f
would be illegal, the compiler would tell you something like "unknown indetifier aChar
in function f
". Using a pointer to that object as done above is completely legal.
The scope of a variable refers to the extent to which different parts of a program have access to the variable. Variables can be declared as:
Inside a function which is called local variables or internal variables.
Outside of all functions which is called global variables or external variables and lifetime or "extent" extends across the entire run of the program.
Here is detailed tutorial about variables with examples : What is variable in C
精彩评论