1

From Scope vs. Lifetime of Variable we see the part of anser from Alok:

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.

That is interesting. Why not remove the variable x once it reach }?

Community
  • 1
  • 1
guo
  • 9,674
  • 9
  • 41
  • 79
  • 2
    "Removing" a variable doesn't mean anything. – Cubic Sep 30 '16 at 09:28
  • Because the compiler can do whatever it wants with your code, as long as the behaviour stays the same. And certain code makes that behaviour undefined. – Zeta Sep 30 '16 at 09:29
  • 6
    The variable is certainly removed, but the memory area it used might not be reused for some time and so still hold the old value. – Bo Persson Sep 30 '16 at 09:29
  • @Cubic it means something. For instance, the beginner of C++ could know that he made a mistake and he did not understand what is variable's lifetime if the compiler gave him a wrong variable at the first time he tried to access the variable that should be removed before. – guo Sep 30 '16 at 09:33
  • @guo Sure, and similar techniques are used for memory sanitizers and related tools, but mandating that for all production code would slow everything down astronomically. – TartanLlama Sep 30 '16 at 09:34
  • @guo I'm not saying that "removing a variable" is _useless_, I'm saying that this is a concept that doesn't exist in C++. A variable can go out of scope, but that doesn't mean it was "removed". – Cubic Sep 30 '16 at 09:39
  • @guo: C++ already offers, what you are asking for. It's called *visibility* (tightly coupled to life time), and when an object goes out of scope, it is no longer visible (and cannot be accessed). So stop using pointers, and no one gets hurt. – IInspectable Sep 30 '16 at 09:43

3 Answers3

4

Same concept as when you delete a file on your hard disk, why does it just delete the index entry rather than format the file?
It takes time.

There's no point in wiping memory if there's no need to do so. The compiler will use that chunk of memory again later when you make another local variable, but until it does so it will just ignore the contents.

UKMonkey
  • 6,941
  • 3
  • 21
  • 30
2

Why not remove the variable x once it reach }?

What do you mean with "remove the variable x?"

The only thing that comes close to "removing" a variable is overwriting it's value at &x. There's no reason for a compiler to do this because this would waste time for nothing. Instead, when a variable reaches the end of it's scope the code can just no longer access x through it's name (and thus it's memory address is no longer valid to the programmer) and the memory address becomes available to the system again. Then, when at one point some other variable is created and needs that address space the value of that variable is just written to &x, overwriting it's previous value.

This is also why it's undefined behaviour to read an unitinitalized variable, a variable declaration like int x; only reserves some space, the address still holds the value that was there earlier and needs to be overwritten by the programmer explicitly ( int x = 0;) .

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
0

When a variable passes out of scope, the compiler is allowed to assume no code will try to access it - since any attempt to do so gives undefined behaviour.

Since the compiler is allowed to assume no code accesses a non-existent variable, it can do what it likes with the memory occupied by that variable after it ceases to exist. It can choose to reclaim the memory. It can simply leave the memory alone, and reallocate it for some other use when it needs to. It can encrypt the contents of the memory if it likes.

Practically, the reason a compiler will not reclaim the memory is performance - there is a performance hit in reclaiming memory, cleaning up that memory to allow it to be reused by other code, etc. And, since the compiler is not required to do any of those things, few compiler vendors will deliberately do something that gives an unnecessary performance hit.

Peter
  • 35,646
  • 4
  • 32
  • 74