1 / 22

Chapter 15 Memory Management:

Chapter 15 Memory Management:. Four main memory areas for a C++ program: Code: code for instructions, methods , etc. static data: Global variables (declared outside scope of any function or those declared as static – see examples on p. 591).

beyla
Download Presentation

Chapter 15 Memory Management:

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 15 Memory Management: • Four main memory areas for a C++ program: • Code: code for instructions, methods, etc. • static data: Global variables (declared outside scope of any function or those declared as static – see examples on p. 591). • Run time stack: locale variables, pass by value parameters (See figure on p. 592). • Heap: for pointers. new operator gets memory from the heap. Sometimes called dynamic memory allocation.

  2. See common errors p. 594. • Avoid dangling pointers p. 596-7. • Avoid buffer overflowsp. 597.

  3. Memory leaks. • See Figure on p. 600 • relates to logic on p. 599; • See code on next slide. • Run with and without the delete p command. • Look at windows task manager (Ctrl-Alt-Delete) and look at process and performance tabs while running.

  4. #include <iostream> #include <sstream> using namespace std; class myClass { private: long data[100000]; public: myClass(inti); long getData(inti); }; myClass::myClass(int i) {data[i]=12;} long myClass::getData(inti) { return data[i];} int main() { myClass *p; for (int i=0; i<10000; i++) { p = new myClass(i); cout << i << " " << p->getData(i) << endl; //cin.get(); //delete p; } cin.get(); return 0; }

  5. Make sure nothing else points to an object when you delete it. • Do not delete an element more than once or one that was never allocated. Example (Remove the comments from the code on the next slide):

  6. class ClassA { private: int a; public: ClassA(int i); int getData(); }; ClassA::ClassA(int i) {a=I;} ClassA::getData() { return a;} int main() { ClassA *p1 = new ClassA(12); ClassA *p2 = p1; cout << p1->getData() << endl; //delete p1; cout << p2->getData() << endl; //delete p2; cin.get(); return 0; }

  7. Demonstrate the code from the snippets file (class Demo). • Discuss the results of the second set of output commands.

  8. Copy constructor (p. 608) • constructor that has one argument – an object of the same class the constructor is in. • It’s used to create a copy of an object.

  9. Explain why the demo does not work as expected and then include the copy constructor below: Demo(const Demo& d) { first = d.getFirst(); second = new int[5]; for (inti=0; i<5; i++) second[i] = d[i]; }

  10. Focus on the code after the statement "y=x". • There is still a problem – and a memory leak. • Objects x and y are sharing dynamic memory. • How do we have x and y correspond to different dynamic memory? • Ans: Overload the “=“ operator.

  11. Demo09 contains the author’s newly define String class. • NOTE: That Visual C++ already has a String class when building a managed application. This demo is unmanaged. • Discuss implementation and run to the printReverse() method. • Problem solve the printReverse() method.

  12. A few things to note: • Note the two overloaded [] operators on pp. 604-5. • Recall the need for two such operators discussed in the previous chapter. • See common error on p.605. • If you do not define a copy constructor there is a System-defined copy constructor. Quality tip on p. 609. • However it might NOT be want you need!!

  13. Uses • When one object is declared and “set equal to” another object of the same class all on the same line (See demo09). • When an object is “passed by value” to a function (See function printReverse() in demo09). • Note: change the function parameter to “pass by reference” and the copy constructor is NOT called. • Or Include the copy constructor on the next slide.

  14. String Copy Constructor String::String(const String& right) { len = right.length(); buffer = new char[len+1]; for (int i=0; i<len; i++) buffer[i] = right[i]; buffer[len] = '\0'; }

  15. SHOULD always define your own copy constructor when your class uses dynamic memory. • Why!! • Use debugger to show what is happening with and without copy constructors.

  16. Shallow copy • One object is a copy of another, including the SAME memory address in pointer variables. • They share dynamically allocated data.

  17. Deep copy • Objects do NOT share dynamically allocated memory • The contents of the dynamically allocated memory are the same in both objects. • One is a copy of the other.

  18. Note how the = operator is overloaded. • The code WILL work without this overloaded operator so why include it? • ANS. To remove any dynamically allocated memory and make sure deep copies are created if that is the intent. • Note the quality tip on p. 612. Can use messages or put in breakpoints. I recommend this!!

  19. Destructors: • never explicitly invoked. Called • at the end of a block when an object goes out of scope. • at the end of functions (for any arguments or locals that were constructed). • When dynamically allocated memory, object variable or object from a derived class is deleted • when main finishes.

  20. Note the difference between destruction and deletion (p. 615). • Note the common error p. 617 – This will be relevant later. • Note the quality tip p. 619. • The Big Three. • Destructors, copy constructors, and assignment operator. • IMPORTANT!!!. If you do not provide them, the system will. The result is may be incorrect!!!

  21. Note code snippets on p. 620. • Exercise: implement that in demo09. • Mention overloading new and delete operators, p. 621. • We will not do this.

  22. Destructor code is important for properly managing garbage collection. • Poorly written destructors can result in memory leaks or dangling pointers. • Delete dynamic memory but only if that memory is not referenced anywhere else. • Discuss Reference Counting, p. 622.

More Related