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

Chapter 15 Memory Management:

  • Upload
    beyla

  • View
    32

  • Download
    0

Embed Size (px)

DESCRIPTION

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). - PowerPoint PPT Presentation

Citation preview

Page 1: 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).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.

Page 2: Chapter 15 Memory Management:

See common errors p. 594.Avoid dangling pointers p. 596-7.Avoid buffer overflows p. 597.

Page 3: Chapter 15 Memory Management:

Memory leaks. See Figure on p. 600relates 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.

Page 4: Chapter 15 Memory Management:

#include <iostream>#include <sstream>using namespace std;class myClass { private: long data[100000]; public: myClass(int i); long getData(int i);};myClass::myClass(int i) {data[i]=12;}long myClass::getData(int i) { 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;}

Page 5: Chapter 15 Memory Management:

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):

Page 6: Chapter 15 Memory Management:

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;}

Page 7: Chapter 15 Memory Management:

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

Page 8: Chapter 15 Memory Management:

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.

Page 9: Chapter 15 Memory Management:

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 (int i=0; i<5; i++)

second[i] = d[i];}

Page 10: Chapter 15 Memory Management:

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.

Page 11: Chapter 15 Memory Management:

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.

Page 12: Chapter 15 Memory Management:

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!!

Page 13: Chapter 15 Memory Management:

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

Page 14: Chapter 15 Memory Management:

String Copy ConstructorString::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';

}

Page 15: Chapter 15 Memory Management:

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.

Page 16: Chapter 15 Memory Management:

Shallow copy

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

Page 17: Chapter 15 Memory Management:

Deep copy

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

Page 18: Chapter 15 Memory Management:

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!!

Page 19: Chapter 15 Memory Management:

Destructors:

never explicitly invoked. Calledat 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 deletedwhen main finishes.

Page 20: Chapter 15 Memory Management:

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!!!

Page 21: Chapter 15 Memory Management:

Note code snippets on p. 620. Exercise: implement that in demo09.

Mention overloading new and delete operators, p. 621.

We will not do this.

Page 22: Chapter 15 Memory Management:

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.