160 likes | 185 Views
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.
E N D
Dynamic Memory Allocation(also see pointers lectures)-L. Grewe
Objectives • What is Dynamic Memory Allocation • How to do memory allocation featuring new.
Dynamic Memory Allocation • At run time to allocate memory using pointers to point to it. • Different ways: • new • malloc(), alloc( ), free ( )
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.
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;
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
? ? 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
? 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
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
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 …
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 ->)
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
Releasing Memory • Some languages like Java, C# have garbage collectors. • In C/C++ you can deallocate memory directly • delete • dealloc , free
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
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
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)