Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
-
Upload
neal-goodness -
Category
Documents
-
view
220 -
download
0
Transcript of Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Dynamic Memory Allocation
• At run time to allocate memory using pointers to point to it.
• Different ways:
• new
• malloc(), alloc( ), free ( )
3
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
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.
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
The new Operator
allocates memory and return a pointer
?? p1p1900900int *p1;
p1 = new int;
*p1 = 20;
?? ??1050010500
- p1 points to a dynamic integer variable - p1 points to a dynamic integer variable without any identifier (name) without any identifier (name)
- dynamic memory comes from the - dynamic memory comes from the programs’ programs’ heapheap (free store) (free store)
2020 ??1050010500
??900900
904904
908908
……
……
1049210492
1049610496
1050010500
p1p1
??
1050010500
2020
Dynamic Arrays
new can allocate an entire array all at once
?? p1p1900900int *p1;
p1 = new int[4];
p1[2] = 20;
cout<<*(p1+2);1048810488
- p1 points to 1st entry of dynamic array- p1 points to 1st entry of dynamic array
- number of entries in a pair of sq. number of entries in a pair of sq. brackets brackets
- two ways to access p1 (array or two ways to access p1 (array or pointer) pointer)
??900900
904904
908908
……
……
1048810488
1049210492
1049610496
1050010500
p1p1
??
1048810488
2020
2020
Accessing Dynamic Array
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;
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
…
Dynamic Objects of a class
new can also allocate a dynamic object
?? p1p1900900point *p1;
p1 = new point(1.0, 2.0);
cout<< (*p1).get_x();
cout<< p1->get_x();
- p1 points to dynamic object without name- p1 points to dynamic object without name
- parameters can be used as in declarationparameters can be used as in declaration
- two ways to access p1 (* and ->)two ways to access p1 (* and ->)
??900900
904904
908908
……
……
1048810488
1049210492
1049610496
1050010500
p1p1
??
1049610496
1.01.0
2.02.0
1049610496 1.01.0 2.02.0
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
13
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
FF
TT
TT
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.
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: