1 / 25

Main Index

CSE 331 – Lecture 8. 1. Main Index. Contents. Chapter 5 Pointers Data Addresses in Memory Declaring Pointer Variables Assigning Values to Pointers Arrays and Pointers Dynamic Memory Management new & delete. The Destructor The Copy Constructor An Overloaded Assignment Operator

jered
Télécharger la présentation

Main Index

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. CSE 331 – Lecture 8 1 Main Index Contents • Chapter 5 • Pointers • Data Addresses in Memory • Declaring Pointer Variables • Assigning Values to Pointers • Arrays and Pointers • Dynamic Memory Management • new & delete • The Destructor • The Copy Constructor • An Overloaded Assignment Operator • The Pointer ‘this’ • The miniVector Class • Matrices • The matrix Class • Summary Slides (3 pages)

  2. Quick Review & Demo • Demonstrate use of g++, make and ddd • Demonstrate Mandelbrot program • Answer questions about next assignment

  3. 3 Main Index Contents Pointer Illustration // x is an int and // ip points to an int int x, *ip; x = 37; the data ip = &x; // fp points to a // dynamically allocated and the pointer // nameless float float *fp; fp = new float; *fp = -87.5;

  4. 4 Main Index Contents Data Addresses in Memory

  5. 5 Main Index Contents Declaring Pointer Variables • Pointer declaration format <type> * <ptr_name>; • The declared pointer is a variable whose value is the address of a data item of the designated type. int *intPtr; char *charPtr;

  6. Assigning Values to Pointers int m = 50, *intPtr; // here intPtr “points to” nothing intPtr = &m; // &m is the address of m // now intPtr “point to” m

  7. 7 Main Index Contents Arrays and Pointers The amount the pointer is incremented is based on the sizeof() the base type of the array Ex: arr+5 is really &arr[0] + 5*sizeof(element_type)

  8. Dynamic Memory • Pointer • Variable whose value is the address of another data value • Heap • System managed store of memory available to be allocated and deallocated while program is running • New • operator to dynamically allocate memory from the heap for program use • Delete • operator to dynamically deallocate memory no longer needed by program and return it to the heap

  9. “new” and “delete” operators • Allocating & deallocating a single data location int *p = new int; // allocates it delete p; // deallocates it • Allocating & deallocating a dynamic array • Note square brackets used with delete operator. These ensure system deallocates ALL locations in array, not just the first one. // allocate space for 300 ints int *arr = new int[300]; // deallocate all 300 int locations delete [] arr;

  10. Pointers and classes • Use # 1 • Dynamically allocate and deallocate class objects • Use # 2 • Dynamically allocate and deallocate storage used by class member variables • Why is this important? • When execution exits a block within which an object is declared, a destructor is called automatically. • The default destructor takes care of returning memory to the heap in usage # 1. • In usage # 2 we must write our own destructor to return the memory used by member variables, else it is lost to the program creating a memory leak.

  11. 11 Main Index Contents Illustrating the Destructor Dynamic class from text (member 1 is data -- member 2 is a pointer)

  12. The destructor // super simple vector class template<typename T> class myVector { public: // the constructor allocates the array myVector(int n=0) : dSize(n), data(NULL) { if (dSize > 0) data = new T(dSize); } // the destructor deallocates the array ~myVector() { if (data != NULL) delete [] data; } private: T *data; // the dynamically allocated array int dSize; // the size of the array };

  13. Assignment & Copy Constructor • C++ provides a default destructor, assignment operator and copy constructor for every class • All of these are “shallow” • They only copy or deallocate the member variables themselves, NOT what they “point to” • For all classes that contain member variables that point to dynamically allocated memory, we must create our own destructor, assignment operator and copy constructor

  14. Shallow Copy // object A has 10 data locations // and B is a copy of A (but BOTH point to // the single dynamically allocated array) myVector<int> A(10), B(A); A B nSize data nSize data dynamically allocated array (10 locations)

  15. Copy Constructor / Overloaded Assignment Operator

  16. Copy Constructors • The Copy Constructor is called whenever …. • 1) objects are passed as value parameters • 2) an object is returned as the function value • 3) an object is created and initialized with another object of the same class

  17. Copy Constructor • Example: expanded myVector class • Note: parameter MUST be a reference parameter, because the copy constructor itself is used in passing by value template<typename T> myVector<T>::myVector(const myVector<T>& v) : dSize(v.dSize) { data = new T[v.dSize]; // allocate space for(int i=0; i<dSize; i++) data[i] = v.data[i]; // copy values }

  18. IS the object *this objA; this-> memberl Points to the Object The Pointer ‘this’

  19. Overloading Assignment • Example: expanded myVector class template<typename T> myVector<T>& myVector<T>::operator=(const myVector<T>& v) { if (*this == v) // copying itself return *this; if (dSize < v.dSize) // existing array too small { delete [] data; // deallocate data = new T[v.dSize]; // allocate enough space } for (int i=0; i<v.dSize; i++) data[i] = v.data[i]; // copy values dSize = v.dSize; return *this; }

  20. miniVector Class • The miniVector class is an implementation of a vector container class illustrating all of these dynamic memory issues • Ford & Topp, Ch 5: d_vector.h

  21. 21 Main Index Contents Matrices • A Matrix is a two-dimensional array that corresponds to a row-column table of entries of a specified data type. • Matrices are referenced using a pair of indices that specify the row and column location in the table. Example: The element mat[0][3] is 2 The element mat[1][2] is 4.

  22. Matrix Class • The matrix class is an implementation of a 2D matrix container class using a vector of vectors • It is dynamically allocated and resizable • Ford & Topp, Ch 5: d_matrix.h • NOTE: This class is NOT complete. It is missing a destructor, copy constructor and assignment operator.

  23. 23 Main Index Contents Summary Slide 1 §- Pointerscontain the address of data in memory… - Data is accessed by applying the dereference operator * §- Operators such as +, ++, and += apply to pointers. §- With such operators, pointers can be used for algorithms involving array traversal, but their primary application is in the allocation and maintenance of dynamic memory.

  24. 24 Main Index Contents Summary Slide 2 §- vector implementation - The miniVector class illustrates the key points. 1) It allocates dynamic memory using: destructor copy constructor overloaded assignment operator 2) It implements push_back(): Therefore it must control vector capacity in order to minimize dynamic memory reallocation. 3) It allows access to elements by using an index: Therefore the class implements an overloaded index operator

  25. 25 Main Index Contents Summary Slide 3 §- Two dimensional arrays in C++ - have the same problems as one-dimensional arrays: 1) fixed size 2) no size attribute 3) If it is a function argument, it is necessary to specify the number of columns as a constant.

More Related