0
void func()
{
    const int intAge = 24;
}

What happens with intAge after you run func()? Do you have to deallocate it, or does the C++ compiler do this?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
olemca
  • 19
  • 10

2 Answers2

3

The storage of variables with automatic storage duration is deallocated automatically when the variable goes out of scope. This is handled by the language implementation.

In fact, there is no need and no way to manually deallocate memory of any variable. Only dynamic memory can be deallocated manually.

eerorika
  • 232,697
  • 12
  • 197
  • 326
2

No. The memory is allocated using stack memory which is automatically free'd after the scope ends. The code to manage stack memory is emitted by the compiler when you build your program.

hgs3
  • 525
  • 3
  • 6
  • To be accurate, the memory is allocated using **automatic storage**, which *may be implemented* using stack memory. The C++ standard makes no mention of "stack" or "heap" memory at all, that is an implementation detail of the compiler based on the platform being targetted. – Remy Lebeau Jan 28 '21 at 18:15
  • @RemyLebeau I see. So the key word is automatic storage. In this case, since it is meant to be built for Windows Console, it is stack memory. Correct? – olemca Jan 28 '21 at 19:30
  • @olemca On Windows, local variables are allocated in stack memory, yes. – Remy Lebeau Jan 28 '21 at 19:38