c++scopeautomatic-storage

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


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?


Solution

  • 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.