Object Lifetime In C C++


Published on

Object Lifetime is critical for understanding C++

  • Be the first to comment

  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Object Lifetime In C C++

  1. 1. February 28, 2009 Object Lifetime in C/C++ Madhubanti Dasgupta Interra Systems (India) Pvt. Ltd. Current Practices in Object Oriented Software Development Meghnad Saha Institute of Technology
  2. 2. Agenda <ul><li>Object Lifetime Fundamentals </li></ul><ul><li>Data Memory Areas </li></ul><ul><li>Types of Objects </li></ul><ul><ul><li>Static Objects </li></ul></ul><ul><ul><li>Automatic Objects </li></ul></ul><ul><ul><li>Dynamic Objects </li></ul></ul>
  3. 3. Object Lifetime Fundamentals Allocation - Deallocation
  4. 4. Object Lifetime Fundamentals <ul><li>Starts with Constructor execution </li></ul><ul><ul><li>As soon as Initialization ends and control enters Constructor Body </li></ul></ul><ul><ul><li>Must follow Memory Allocation </li></ul></ul><ul><li>Ends with Destructor execution </li></ul><ul><ul><li>As soon as control leaves Destructor Body </li></ul></ul><ul><ul><li>Must precede Memory De-allocation </li></ul></ul>
  5. 5. Object Lifetime Fundamentals <ul><li>class MyClass; </li></ul><ul><li>void MyFunc() // Allocation on Stack for myObj </li></ul><ul><li>{ </li></ul><ul><li>MyClass myObj; // Construction </li></ul><ul><li>// MyClass::MyClass() </li></ul><ul><li>... </li></ul><ul><li>myObj.myMethod(); // Use </li></ul><ul><li>... </li></ul><ul><li>return; // Destruction - Implicit </li></ul><ul><li>// myObj.~MyClass() </li></ul><ul><li>} // De-Allocation from Stack for myObj </li></ul>
  6. 6. Object Lifetime Fundamentals <ul><li>Stages </li></ul><ul><ul><li>Memory Allocation & Binding </li></ul></ul><ul><ul><li>Constructor Call & Execution </li></ul></ul><ul><ul><li>Object Use </li></ul></ul><ul><ul><li>Destructor Call & Execution </li></ul></ul><ul><ul><li>Memory De-Allocation & De-Binding </li></ul></ul>Managed Lifetime
  7. 7. Object Lifetime Fundamentals <ul><li>Built-in / Pre-Defined Types </li></ul><ul><ul><li>No Explicit Constructor / Destructor </li></ul></ul><ul><ul><li>Compiler optimizes these calls </li></ul></ul><ul><ul><li>Same pattern for the Creation / Destruction processes </li></ul></ul>
  8. 8. Data Memory Areas System Agnostic View
  9. 9. Data Memory Areas Program Code Text Global / File Scope Objects; Static Class Scope Objects; Function Scope Objects Static Dynamic Objects managed by new / delete & malloc / free Free Store & Heap Automatic Objects Stack Constant literals including strings Const Data Data Area
  10. 10. Types of Objects Static – Automatic – Dynamic
  11. 11. Static Objects Compiler-Managed Objects in the Global Store
  12. 12. Static Objects <ul><li>Objects in Static Memory </li></ul><ul><li>Constructed only once at Program Startup </li></ul><ul><ul><li>Function Objects created on first execution </li></ul></ul><ul><li>Destructed on Normal Program Termination </li></ul><ul><ul><li>return or exit() </li></ul></ul><ul><ul><li>Uses atexit() Registration to call Destructors </li></ul></ul><ul><li>Static Objects are </li></ul><ul><ul><li>Global and Namespace Objects </li></ul></ul><ul><ul><li>Class Objects with the static modifier </li></ul></ul><ul><ul><li>Function Objects with the static modifier </li></ul></ul><ul><li>Memory requirements handled by the compiler </li></ul><ul><ul><li>Offsets / Layout statically computed </li></ul></ul>
  13. 13. Static Objects <ul><li>static MyClass obj1; // Global static object </li></ul><ul><li>MyClass MyClass::obj2; // Class static object </li></ul><ul><li>void staticFunc() </li></ul><ul><li>{ </li></ul><ul><li>static MyClass obj3; // Function static object </li></ul><ul><li>{ </li></ul><ul><li>static MyClass obj4; // Block static object </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  14. 14. Static Objects <ul><li>Global (or File) / Namespace Scope / Class Scope </li></ul><ul><ul><li>Built-in Types </li></ul></ul><ul><ul><ul><li>Has no boundary for translation units – literally “load time” </li></ul></ul></ul><ul><ul><ul><li>Object created (& initialized) implicitly at program load </li></ul></ul></ul><ul><ul><ul><ul><li>before first assembly instruction in the program </li></ul></ul></ul></ul><ul><ul><ul><ul><li>actually before any global object of user-defined type is constructed </li></ul></ul></ul></ul><ul><ul><ul><li>Object destroyed implicitly before the program unload </li></ul></ul></ul><ul><ul><ul><ul><li>after last assembly instruction in main() </li></ul></ul></ul></ul><ul><ul><ul><ul><li>actually after all global objects of user-defined type are destructed </li></ul></ul></ul></ul><ul><ul><li>User-Defined Types </li></ul></ul><ul><ul><ul><li>Space allocation is done at “load-time” </li></ul></ul></ul><ul><ul><ul><li>Object created (and initialized) </li></ul></ul></ul><ul><ul><ul><ul><li>sequentially within a translation unit </li></ul></ul></ul></ul><ul><ul><ul><ul><li>arbitrarily across translation units </li></ul></ul></ul></ul><ul><ul><ul><li>Objects destroyed in the reverse order of creations </li></ul></ul></ul>
  15. 15. Static Objects <ul><li>Local Function / Block Scope </li></ul><ul><ul><li>Built-in Types </li></ul></ul><ul><ul><ul><li>Same as static objects in Global Scope </li></ul></ul></ul><ul><ul><li>User-Defined Types </li></ul></ul><ul><ul><ul><li>Space allocation is done at “load-time” </li></ul></ul></ul><ul><ul><ul><li>Object created (and initialized) when the control passes the definition for the first time </li></ul></ul></ul><ul><ul><ul><li>Destroyed in the reverse order of creations (LIFO) </li></ul></ul></ul><ul><ul><ul><ul><li>after last assembly instruction in main() </li></ul></ul></ul></ul><ul><ul><ul><ul><li>before the global objects of user-defined type are destructed </li></ul></ul></ul></ul><ul><ul><ul><li>Uses atexit() – because the creation order is dynamic </li></ul></ul></ul>
  16. 16. Execution beyond main() <ul><li>static MyClass *obj; // Global Static Object Ptr </li></ul><ul><li>void clean() { delete obj; } // Wrapper Function </li></ul><ul><li>void staticFunc() </li></ul><ul><li>{ </li></ul><ul><li>obj = new MyClass(); // Construction </li></ul><ul><li>atexit(clean); // atexit() Registration </li></ul><ul><li>} </li></ul>
  17. 17. Execution beyond main() <ul><li>atexit() signature </li></ul><ul><ul><ul><li>int atexit(void (*pFunction)(void)); </li></ul></ul></ul><ul><li>Register any function having the following signature </li></ul><ul><ul><ul><li>void CallbackFunction(void); </li></ul></ul></ul><ul><li>Registered function called at runtime after main() exits </li></ul><ul><li>For Multiple Registrations, calls are made in the reverse order of registration or LIFO </li></ul><ul><li>Used by </li></ul><ul><ul><li>Compiler for Local Static Objects </li></ul></ul><ul><ul><li>Application Programmer for any function call beyond main() </li></ul></ul><ul><li>return() or exit() invokes all Callbacks </li></ul><ul><li>abort() bypasses all Callbacks </li></ul>
  18. 18. Automatic Objects Compiler-Managed Objects in Local Store
  19. 19. Automatic Objects <ul><li>Objects in Stack (Local) Memory </li></ul><ul><li>Objects Managed via Static Scopes </li></ul><ul><ul><li>Function Scope </li></ul></ul><ul><ul><li>Block Scope within a Function Scope </li></ul></ul><ul><ul><li>Block Scope within another Block Scope </li></ul></ul><ul><li>Objects Laid out in the Stack Frame of the Function Scope </li></ul><ul><ul><li>Constructed when the control passes the declaration </li></ul></ul><ul><ul><li>Destructed when (just after) the control leaves the scope </li></ul></ul><ul><ul><ul><li>Normal Exit: return / break / goto </li></ul></ul></ul><ul><ul><ul><li>Abnormal Exit: Exception (throw) </li></ul></ul></ul><ul><ul><li>Objects (within a scope) are destroyed in the reverse order of creations </li></ul></ul><ul><li>Memory requirements handled by the Compiler </li></ul>
  20. 20. Automatic Objects <ul><li>void automaticFunc() </li></ul><ul><li>{ </li></ul><ul><li>MyClass obj1; // Function Scoped </li></ul><ul><li>MyClass *obj2 = new MyClass(); </li></ul><ul><li>{ MyClass obj3; } // Block Scoped </li></ul><ul><li>if (MyClass obj4()) { delete obj2; } // if Scoped </li></ul><ul><li>else { } </li></ul><ul><li>} </li></ul>
  21. 21. Automatic Objects <ul><li>Automatic Objects are </li></ul><ul><ul><li>Function Scope Objects </li></ul></ul><ul><ul><ul><li>Formal Parameters </li></ul></ul></ul><ul><ul><ul><li>Return Values </li></ul></ul></ul><ul><ul><li>Block Scope Objects </li></ul></ul><ul><ul><ul><li>Compound Statements (like ‘for’ or ‘switch’ or ‘while’ or ‘do-while’) </li></ul></ul></ul><ul><ul><li>Non-static Data Member Objects in a class </li></ul></ul><ul><ul><ul><li>As part of the Parent Object’s Layout </li></ul></ul></ul><ul><ul><li>Base class Object in a class </li></ul></ul><ul><ul><ul><li>As part of the Derived Object’s Layout </li></ul></ul></ul><ul><ul><li>Temporary Objects </li></ul></ul><ul><ul><ul><li>Unnamed Objects </li></ul></ul></ul><ul><ul><ul><li>Compiler Generated & Managed </li></ul></ul></ul>
  22. 22. Dynamic Objects User-Managed Objects in the Free Store
  23. 23. Dynamic Objects <ul><li>Objects in Free Store Memory </li></ul><ul><li>Memory allocated dynamically </li></ul><ul><ul><li>Constructor called by operator new / placement new </li></ul></ul><ul><ul><ul><li>Must follow Memory Allocation </li></ul></ul></ul><ul><ul><li>Destructor called by operator delete </li></ul></ul><ul><ul><ul><li>Must precede Memory De-allocation </li></ul></ul></ul><ul><ul><li>Lifetime controlled by the user </li></ul></ul><ul><ul><ul><li>Transformed to scoped management by Smart Pointers </li></ul></ul></ul><ul><li>Memory requirements handled by the User </li></ul><ul><li>Lifetime Managed by the User </li></ul>
  24. 24. Dynamic Objects <ul><li>void dynamicFunc() </li></ul><ul><li>{ </li></ul><ul><li>MyClass *obj = new MyClass(); // Dynamic Object </li></ul><ul><li>int offset = sizeof (MyClass); </li></ul><ul><li>char *plBuf = new char [2 * offset]; // Allocation </li></ul><ul><li>MyClass *plObj1 = new (plBuf) MyClass(); // Construction </li></ul><ul><li>MyClass *plObj2 = new (plBuf + offset) MyClass(); </li></ul><ul><li>plObj1->~MyClass(); // Destruction </li></ul><ul><li>plObj2->~MyClass(); </li></ul><ul><li>delete [] plBuf; // Deallocation </li></ul><ul><li>} </li></ul><ul><li>// Dynamic Object ‘obj’ Leaks Memory </li></ul>
  25. 25. Thank You