Scope vs. Lifetime of Variable

2020-01-24 02:40发布

问题:

What is the relation between the scope and the lifetime of a variable? If a variable is out of scope, is the memory of it allowed to be overwritten by another variable, or is the space reserved until the function is left.

I am aksing because I want to know whether the code below actually works, or whether it can be that *p might be undefined

foo() {
  int *p;
  {
    int x = 5; 
    p = &x;
  }
  int y = *p;


}

回答1:

What is Scope?

Scope is the region or section of code where a variable can be accessed.

What is a lifetime?

Lifetime is the time duration where an object/variable is in a valid state.

For, Automatic/Local non-static variables Lifetime is limited to their Scope.
In other words, automatic variables are automagically destroyed once the scope({,}) in which they are created ends. Hence the name automatic to begin with.

What is wrong in your code example?

So Yes your code has an Undefined Behavior.

In your example scope of *p is entire function body after it was created.
However, x is a non-static local/automatic variable and hence the lifetime of x ends with it's scope i.e the closing brace of } in which it was created, once the scope ends x does not exist. *p points to something that doesn't exist anymore.

Note that technically x does not exist beyond its scope however it might happen that the compiler did not remove the contents of x and one might be able to access contents of x beyond its scope through a pointer(as you do).However, a code which does this is not a valid C++ code. It is a code which invokes Undefined Behaviour. Which means anything can happen(you might even see value of x being intact) and one should not expect observable behaviors from such a code.



回答2:

C11 Section 6.2.1 Paragraph 2

For each different entity that an identifier designates, the identifier is visible (i.e., can be used) only within a region of program text called its scope. Different entities designated by the same identifier either have different scopes, or are in different name spaces

.

C11 Section 6.2.4 Paragraph 2

The lifetime of an object is the portion of program execution during which storage is guaranteed to be reserved for it.

In your case x is local to the block, and so is its lifetime, therefore x cannot be accessed by its name outside the block, also because its lifetime is limited to its block the address of x will not be reserved anymore after leaving the block, thus will result in undefined behaviour.

On the other hand, for example, say a static local variable, in this case the scope is local to the block and so we can't access by its name outside the block, but the lifetime is the entire program, so we can use the address of the variable anywhere in the program while it is running. This example should help get the difference.



回答3:

Objects (i.e. the actual underlying things that store values) have lifetimes.

Variables (i.e. the things used to refer to objects) have scope.

Either way, y = *p invokes undefined behaviour; the object referred to by x is automatic, its lifetime ends when x goes out of scope.



回答4:

What is the relation between the scope and the lifetime of a variable?

As Oli stated, variables have scope and objects lifetime. The scope of a variable and the lifetime of it are bound, but you can have objects whose lifetime extends beyond the scope on which they were created:

int* f() {
   int *p                    // Variable p
          = new int(1);      // object (call it x)
   return p;                 // p scope ends, p lifetime ends, x survives
}
int main() {
   int *q                    // Variable q
          = f();             // ... points to x
   delete q;                 // x lifetime ends, q is in scope, q is alive
}

In your particular case, x variable ends it's lifetime when the scope in which it was created is closed, so you have undefined behavior.

If a variable is out of scope, is the memory of it allowed to be overwritten by another variable, or is the space reserved until the function is left.

This is a detail of implementation. Accessing the variable is undefined behavior in all cases and because not all cases must be equal, and if the variable had a non trivial destructor, it would be called at the end of the scope, so whether the memory is there or not is irrelevant: the object is no longer there. That being said, in many cases compilers will not release the memory in the function (i.e. they will not reseat the frame pointer) when a variable goes out of scope, but they might reuse the same space to hold other variables in the same function.



回答5:

I have run your program and the output is 5.The output is still 5 though the x variable is destroyed after the second '}' is because the memory location is not overwritten by any other variable.if you have a lot of code after after the end of second scope,It is highly likely that the memory location previously owned by 'x' is overwritten.

int x = 5; 
*p = &x;
}  //  x lifetime ends after this.but the number '5' is still there in that memory.
   //but it is possible to assign this memory (which previously belong to x) to a new var.


回答6:

Standard defines that the memory is allowed to be overwritten once scope is left. Compiler implementation makes it reserved and freed. Compile-time optimizations might move allocation of stack so they will no longer reflect your order.

Your code does invoke undefined behaviour and should not be used. Your code might actually work, since writing value of y occurs AFTER taking the value p points to.



标签: c++ scope