Do you have to deallocate a const int if you declared it inside a function?

303 views Asked by At
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?

2

There are 2 answers

0
eerorika On BEST ANSWER

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.

3
hgs3 On

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.