Presentation is loading. Please wait.

Presentation is loading. Please wait.

Memory Management Object Life Cycle:  Construction  Allocation  Preinitialization  Initialization  Use  Destruction  Cleanup  Post cleanup  Deallocation.

Similar presentations


Presentation on theme: "Memory Management Object Life Cycle:  Construction  Allocation  Preinitialization  Initialization  Use  Destruction  Cleanup  Post cleanup  Deallocation."— Presentation transcript:

1 Memory Management Object Life Cycle:  Construction  Allocation  Preinitialization  Initialization  Use  Destruction  Cleanup  Post cleanup  Deallocation  Object Lifetime  Static Objects  Automatic Objects  Dynamic Objects  Preventing Dangling References and Garbage

2 Object Lifetime  Static objects allocated once and not freed until program termination.  Automatic objects allocated when their declarations are executed and freed automatically when the block containing them terminates.  Dynamic objects allocated and freed in arbitary order under the programmers control.

3 Class Noisy to trace object con/de struction #include "Noisy.h" Noisy func(Noisy n) { std::cout<< "inside func\n"; return n;} int main() { Noisy x("x"); std::cout<<"\ncalling func\n"; Noisy c = func(x); std::cout<<"after func\n"; std::cout << '\n'; return 0; }

4 constructing: (name=x id=0) calling func copy construct: (name=x id=1) from: (name=x id=0) inside func copy construct: (name=x id=2) from: (name=x id=1) destroying: (name=x id=1) after func destroying: (name=x id=2) destroying: (name=x id=0)

5 Noisy.h #ifndef NOISY_H #define NOISY_H #include class Noisy { static long objects_created; long id; std::string name; public: Noisy(std::string n=""): id(objects_created++), name(n) { if (name == "") { std::ostringstream n; n << "obj_" << id ; name = n.str(); } std::cout<<"constructing: " << *this << '\n'; }

6 Noisy(const Noisy& n): id(objects_created++), name(n.name) { std::cout<<"copy constuct: " << *this << " from: " << n << '\n'; } ~Noisy() {std::cout<<"destroying: " << *this << std::endl;} Noisy& operator=(const Noisy& rhs) { std::cout<<"assignment: " << *this << " = " << rhs << '\n'; name = rhs.name; return *this; } friend std::ostream& operator<<(std::ostream& os, const Noisy &n) { return os << "(name=" << n.name << " id=" << n.id << ')'; } }; // class Noisy long Noisy::objects_created = 0; #endif

7 Static Objects  are allocated once and are not freed until the program terminates.  Scope:  class scope class A {...; static int a;...}; A::a = 1;  file scope with external linkage extern int a; int a = 1;  file scope with internal linkage static int a;  local scope {...; static int a = 1;...}

8 Use the extern keyword to declare file scope names with external linkage; omit the extern keyword on their definitions. extern int a; // Declaration, external linkage extern Complex a; // Decalration, external linkage int a = 1; // Definition, initialized to 1 Complex c1; // Definition, default constructor Avoid file scope objects with external linkage; use class scop instead. extern const double c; extern const double k; const double c = 3.00e8; const double k = 1.38e-23; namespace PhysicalConstants { public: static const double c;... } const double PhysicalConstants::c = 3.00e8;... Prefer namespace scope names to file scope names.

9 static Noisy s1("s1"); // File scope static object. void fnc(int j) { cout << "-- starting fnc(" << j << ") --" << endl; static Noisy s2("s2"); if (j == 2) { static Noisy s4("s4"); } for (int i = 1; i <= 2; i++) { cout << "-- loop i=" << i << " --" << endl; static Noisy s3("s3"); } cout << "-- returning from fnc(" << j << ") --" << endl; } int main() { cout << "-- main starts --" << endl; fnc(1); fnc(2); cout << "-- main ends --" << endl; return 0; } static Noisy s5("s5"); // File scope static object.

10 Automatic Objects int main() { cout << "-- main starts --" << endl; Noisy a1("a1"); for (int i = 1; i <= 2; i++) { cout << " -- loop i= " << i << " -- " << endl; Noisy a2("a2"); // Object created each loop iteration. if (i == 2) { Noisy a3("a3"); // Object created if i == 2. } cout << "-- loop is done -- " << endl; Noisy a4("a4"); cout << "-- main ends --" << endl; return 0; };

11 void f2() { cout << "-- f2 starts -- " << endl; Noisy c("c"); throw "exception"; cout << "-- f2 ends -- " << endl; } void f1() { cout << "-- f1 starts -- " << endl; Noisy a("a"); f2(); Noisy b("b"); cout << "-- f1 ends -- " << endl; } int main() { cout << "-- main starts --" << endl; try { f1(); } catch(const char*) { cout << "Exception caught." << endl; } cout << "-- main ends --" << endl; return 0; }

12 Dynamic Objects  Construction int* p = new int; int* q = new int(3); Rational* r = new Rational(8,9); int* a = new int[n];  Destruction delete p; delete q; delete r; delete [] a;  The worst problems arise from bad deletions:  deleting an object more than once  deleting a pointer not obtained by new  using the wrong form of new Provide a default constructor for every class possible. Match every invocation of new with exactly one invocation of delete of the same kind.

13 Preventing Dangling References and Garbage  Dangling Class Members class Dangle { public: Dangle() { p = new int(0);} ~Dangle() { delete p; } private: int* p; }; void f() { Dangle a; Dangle b = a; } Dangle a Dangle b : : int 0 int* p : int* int* p : int* Provide a copy constructor and an assignment operator for classes with a pointer data member that is deleted by the destructor.

14  Dangling Pointers to Automatic Objects void dangle(int j) { int* p; if (j < 100) { // Dangling reference created when this block terminates int iarray[100]; p = iarray; } else p = new int[j]; for (int i = 0; i < j; i++) p[i] = i; } Avoid pointers to automatic objects.

15  Pointers Left Dangling by Function Calls void f(int* x) { //... delete [] x; // Causes dangling reference } void dynamic_dangle(int size) { int* iarray = new int [size]; f(iarray); for (int i = 0; i < size; i++) cout << iarray[i]; } Don't delete a functions argument.

16  Garbage Memory / Memory Leaks (failing to delete) is the opposite of the dangling reference problem. Hold pointers in class objects and pair new and delete in the class's constructors and destructors.


Download ppt "Memory Management Object Life Cycle:  Construction  Allocation  Preinitialization  Initialization  Use  Destruction  Cleanup  Post cleanup  Deallocation."

Similar presentations


Ads by Google