Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the C++ runtime system know when objects go out of scope

I was wondering how the C++ runtime system detects when an object goes out of scope so that it calls the destructor accordingly to free up the occupied memory.

Thanks.

like image 957
cpp_noname Avatar asked Mar 26 '11 20:03

cpp_noname


People also ask

What happens when a variable goes out of scope?

A variable declared inside a function has a function scope. It has been allocated memory when the function is called and once the function returns something the function execution ends and with it the variable goes out of scope i.e. it gets deleted from the memory.

How does an object go out of scope?

Any local variable defined within a frame will go out of scope once the program exits that frame. When a stack variable goes out of scope, its destructor is called.

What happens when a variable goes out of scope C++?

Nothing physical happens. A typical implementation will allocate enough space in the program stack to store all variables at the deepest level of block nesting in the current function. This space is typically allocated in the stack in one shot at the function startup and released back at the function exit.

What is typically called when an object in C++ goes out of scope?

A destructor is called for a class object when that object passes out of scope or is explicitly deleted.


3 Answers

The runtime doesn't - the compiler keeps tabs on scope and generates the code to call the destructor. If you make a simple test application and look at the generated disassembly you'll see explicit destructor calls.

Disassembly snippet from MSVC:

int main() {
    std::string s1;
...
00971416  call        dword ptr [__imp_std::basic_string<char,std::char_traits<char>,std::allocator<char> >::basic_string<char,std::char_traits<char>,std::allocator<char> > (979290h)] 
...
    {
        std::string s2;
00971440  call        dword ptr [__imp_std::basic_string<char,std::char_traits<char>,std::allocator<char> >::basic_string<char,std::char_traits<char>,std::allocator<char> > (979290h)] 
...    
    }
00971452  call        dword ptr [__imp_std::basic_string<char,std::char_traits<char>,std::allocator<char> >::~basic_string<char,std::char_traits<char>,std::allocator<char> > (979294h)] 
...
}
0097146B  call        dword ptr [__imp_std::basic_string<char,std::char_traits<char>,std::allocator<char> >::~basic_string<char,std::char_traits<char>,std::allocator<char> > (979294h)] 
like image 175
Erik Avatar answered Oct 31 '22 19:10

Erik


This is known statically at compile time

{
  string s; /* ctor called here */
} /* dtor called here */

Sometimes, it's more difficult

{
  again:
  {
    string s; /* ctor called here */
    goto again; /* now dtor of s is called here */
    string q; /* ctor not called. not reached. */
  } /* dtor of s and q would be called here. but not reached */
}
like image 38
Johannes Schaub - litb Avatar answered Oct 31 '22 20:10

Johannes Schaub - litb


It has nothing to do with runtime. The compiler keeps track of scope of every lexical variable, and adds destructor calls.

like image 22
Ben Voigt Avatar answered Oct 31 '22 21:10

Ben Voigt