1 / 16

Dynamic Memory Allocation (also see pointers lectures) -L. Grewe

Learn about dynamic memory allocation using pointers, new operator, & delete operator in C++. Understand dynamic arrays, objects, potential memory leaks, & releasing memory. Good practices and examples included.

Download Presentation

Dynamic Memory Allocation (also see pointers lectures) -L. Grewe

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. Dynamic Memory Allocation(also see pointers lectures)-L. Grewe

  2. Objectives • What is Dynamic Memory Allocation • How to do memory allocation featuring new.

  3. Dynamic Memory Allocation • At run time to allocate memory using pointers to point to it. • Different ways: • new • malloc(), alloc( ), free ( )

  4. Dynamic Allocation using new • The object is stored in a large free memory area called the heap (or free-store). • When created in this way, the object remains on the heap until you remove it. • The delete operator erases the object from the heap.

  5. Creating an Object – allocation via new Using the new operator, we create an int object on the heap and assign its address to P. • int * P = new int; Now we can use the pointer in the same way as previous examples. *P = 25; // assign a value cout << *P << endl;

  6. new and delete The new operator returns the address of a new object. The delete operator erases the object and makes it unavailable. • Student * pS = new Student; • . • . • // use the student for a while... • . • . • delete pS; // gone! Student constructor called

  7. ? ? 10500 ? p1 900 20 ? 10500 The new Operator • allocates memory and return a pointer 10500 int *p1; p1 = new int; *p1 = 20; - p1 points to a dynamic integer variable without any identifier (name) - dynamic memory comes from the programs’ heap (free store) 20

  8. ? p1 900 Dynamic Arrays • new can allocate an entire array all at once 10488 int *p1; p1 = new int[4]; p1[2] = 20; cout<<*(p1+2); 20 10488 • - p1 points to 1st entry of dynamic array • number of entries in a pair of sq. brackets • two ways to access p1 (array or pointer) 20

  9. Use array notation the 1st entry p1[0] = 18; the 3rd entry p1[2] = 20; the ith entry p1[i-1] = 19; Use pointer notation the 1st entry *p1 = 18; the 3rd entry *(p1+2) = 20; the ith entry *(p1+i-1) = 19; Accessing Dynamic Array

  10. Dynamic Array Example • A program read ages of each student in a CS class, with varying sizes, calculate the average, and then print out the average. size_t size; int *ages; float average; cin >> size; ages = new int[size]; // input ages of all students // calculate average // print average …

  11. 10496 10496 1.0 2.0 ? p1 900 1.0 2.0 Dynamic Objects of a class • new can also allocate a dynamic object point *p1; p1 = new point(1.0, 2.0); cout<< (*p1).get_x(); cout<< p1->get_x(); • - p1 points to dynamic object without name • parameters can be used as in declaration • two ways to access p1 (* and ->)

  12. Failure of the new Operator • Dynamic memory via new operator comes from heap of a program • Heap size from several K to GB, however fixed • Could run out of room therefore cause a bad_alloc exception • error message and program halts • Good practice 1: document which functions uses new • Good practice 2: garbage collection by delete operator

  13. Releasing Memory • Some languages like Java, C# have garbage collectors. • In C/C++ you can deallocate memory directly • delete • dealloc , free

  14. The delete Operator • Release any dynamic memory (heap memory) that is no longer needed … delete i_ptr; delete [ ] d_ptr; // empty brackets delete p_ptr; int *i_ptr; double *d_ptr; point *p_ptr; i_ptr = new int; d_ptr = new double[20]; p_ptr = new point(1.0, 2.0); … … • Questions( true or false): • delete resets these pointers • delete removes dynamic objects pointed by the pointers • nothing happens to the pointers themselves F T T

  15. Using new in Functions If you create an object inside a function, you may have to delete the object inside the same function. In this example, variable pS goes out of scope at the end of the function block. • void MySub() • { • Student * pS = new Student; • // use the Student for a while... • delete pS; // delete the Student • } // pS disappears

  16. Memory Leak with Objects is an error condition that is created when an object is left on the heap with no pointer variable containing its address. This might happen if the object's pointer goes out of scope: • void MySub() • { • Student * pS = new Student; • // use the Student for a while... • } // pS goes out of scope • (the Student's still left on the heap)

More Related