Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Dynamic Memory Allocation (also see pointers lectures) -L. Grewe."— 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 ( ) 3

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 int * P = new int; Using the new operator, we create an int object on the heap and assign its address to P. *P = 25; // assign a value cout << *P << endl; Now we can use the pointer in the same way as previous examples.

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

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

8 Dynamic Arrays new can allocate an entire array all at once?p1900 int *p1; p1 = new int[4]; p1[2] = 20; cout<<*(p1+2); p1 points to 1st entry of dynamic array - 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) ? … … p1?

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

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 Dynamic Objects of a class new can also allocate a dynamic object?p1900 point *p1; p1 = new point(1.0, 2.0); cout<< (*p1).get_x(); cout get_x(); - p1 points to dynamic object without name - p1 points to dynamic object without name - parameters can be used as in declaration - two ways to access p1 (* and ->) ? … … p1?

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 13

14 The delete Operator Release any dynamic memory (heap memory) that is no longer needed 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); … delete i_ptr; delete [ ] d_ptr; // empty brackets delete p_ptr; Questions( true or false): 1.delete resets these pointers 2.delete removes dynamic objects pointed by the pointers 3.nothing happens to the pointers themselves FTT

15 Using new in Functions void MySub() { Student * pS = new Student; // use the Student for a while... delete pS; // delete the Student } // pS disappears 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.

16 Memory Leak with Objects 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) 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:


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

Similar presentations


Ads by Google