Download presentation
Presentation is loading. Please wait.
1
Dynamic Objects
2
Memory Management Static Memory Allocation Dynamic Memory
Memory is allocated at compiling time Dynamic Memory Memory is allocated at running time { int a[200]; … } { int n; cin >> n; a[n]??? }
3
Conceptual View of Memory
(DYNAMIC MEMORY)
4
Static vs. Dynamic Objects
Static object Memory is acquired automatically Memory is returned automatically when object goes out of scope Dynamic object Memory is acquired by program with an allocation request new operation Dynamic objects can exist beyond the function in which they were allocated Object memory is returned by a deallocation request delete operation
5
Memory Allocation new delete { int* ptr; int a[200];
… } int* ptr; ptr = new int[200]; … delete [] ptr;
6
Creating a variable (object)
Syntax ptr = new SomeType; where ptr is a pointer of type SomeType Example int* p = new int; p Uninitialized int variable ‘new’ does two things: Create a variable of given type Point the pointer p to the variable
7
Abracadabra!
8
Destructing a variable (object)
Syntax delete p; storage pointed to by p is returned to free store and p is now undefined Example int* p = new int; *p = 10; delete p; ‘delete’ does two things: Return the pointed object to the system Point the pointer p to NULL p 10
9
Dynamic Arrays (a collection of variables)
Syntax for allocation: p = new SomeType[Expression]; Where p is a pointer of type SomeType Expression is the number of objects to be constructed -- we are making an array Syntax for de-allocation: delete [] p; Because of the flexible pointer syntax, p can be considered to be an array
10
Example Dynamic Memory Allocation p p p
Request for “unnamed” memory from the Operating System int* p, n=10; p = new int; p = new int[100]; p p p = new int[n]; p
11
Freeing (or deleting) Memory
12
Allocation Example Need an array of unknown size int grades[n]; main()
{ int n; cout << “How many students? “; cin >> n; int* grades = new int[n]; for(int i=0; i < n; i++){ int mark; cout << “Input Grade for Student” << (i+1) << “ ? :”; cin >> mark; grades[i] = mark; } . . . printMean( grades, n ); // call a function with dynamic array
13
Dangling Pointer Problem
int* A = new int[5]; for(int i=0; i<5; i++) A[i] = i; int* B = A; delete[] A; B[0] = 1; // illegal!
14
Memory Leak Problem int* A = new int[5]; for(int i=0; i<5; i++)
A[i] = i; A = new int[5]; A 1 2 2 3 4
15
Summary Static variables (objects) Dynamic variables (objects)
A (direct) named memory location A static part (pointer) + (indirect) nameless memory location (dynamic part) int a; a = 20; int* pa; pa = new int; *pa = 20; 20 20 a pa static dynamic static
16
int* p = new int; int* p = new int[100]; *p = 10;
Dynamic array Simple dynamic variable int* p = new int; *p = 10; delete p; int* p = new int[100]; for (i=1;i<100,i++) p[i] = 10; delete[] p; 10 10 p 10 p 10 ‘delete’ two actions: Return the object pointed to Point the pointer p to NULL ‘delete p’ is not sufficient for an array!!!
17
Good practice Logically, Initialize a pointer with NULL
Always check a pointer before usage if (p != NULL) *p
18
n=100; int* p = new int[n]; { int n; cin >> n; N=150; a[n]???
Bad examples n=100; int* p = new int[n]; N=150; P[130]??? { int n; cin >> n; a[n]??? } It’s possible, but not standard, Do: int* a = new int[n]; No! the array stays with the size 100.
19
Concept of lists
20
A simple ‘list’ example
Concept of a list, e.g. a list of integers A list is a linear sequence of objects Print out info Empty test Search an element Insertion (at head, at end, any position) Deletion … implemented by a static array (over-sized if necessary) int list[1000]; int size; by a dynamic array int list[size]; int size; by a linked list and more …
21
How to use a list? Static array! const int DIM=1000; int main() {
int A[DIM]; int n; cin >> n; … } int main() { int A[1000]; int n; cin >> n; initialize(A, n, 0); print(A, n); A = addEnd(A,n,5); A = addHead(A,n,5); A = deleteFirst(A,n); selectionSort(A, n); } Static array!
22
Initialize void initialize(int list[], int size, int value){
for(int i=0; i<size; i++) list[i] = value; }
23
Print out a list void print(int list[], int size) {
cout << "[ "; for(int i=0; i<size; i++) cout << list[i] << " "; cout << "]" << endl; }
24
Delete the first element
// for deleting the first element of the array void deleteFirst(int list[], int& size){ for(int i=0; i<size-1; i++) list[i] = list[i+1]; size--; } deleteFirst(A,n) int* deleteFirst(int list[], int& size){ for(int i=0; i<size-1; i++) list[i] = list[i+1]; size--; return list; } A = deleteFirst(A,n) Pointer type!
25
Adding Elements addEnd(A,n,v) If not full! A = addEnd(A,n,v)
// for adding a new element to end of array void addEnd(int list[], int& size, int value){ if (size < DIM) { list[size] = value; size++; } addEnd(A,n,v) If not full! int* addEnd(int list[], int& size, int value){ if ((size>0) && (size < DIM)) { list[size] = value; size++; } return list; A = addEnd(A,n,v)
26
Add at the beginning: addHead(A,n,v) A = addHead(A,n,v)
// for adding a new element at the beginning of the array void addHead(int list[], int& size, int value){ if(size < DIM){ for(int i=size-1; i>0; i--) list[i+1] = list[i]; list[0] = value; size++; } else cout << “out of array memory!!! << endl; addHead(A,n,v) int* addHead(int list[], int& size, int value){ if(size < DIM){ for(int i=size-1; i>0; i--) list[i+1] = list[i]; list[0] = value; size++; return list; } else … A = addHead(A,n,v)
27
Using a dynamic array int main() { cout << "Enter list size: ";
int n; cin >> n; int* A = new int[n]; … }
28
How to use a list? int A[1000]; int n; cin >> n; int main() {
cout << "Enter list size: "; int n; cin >> n; int* A = new int[n]; initialize(A, n, 0); print(A, n); A = addEnd(A,n,5); A = addHead(A,n,5); A = deleteFirst(A,n); selectionSort(A, n); delete[] A; } int A[1000]; int n; cin >> n;
29
Initialize and Print-out: the same as before
void initialize(int list[], int size, int value){ for(int i=0; i<size; i++) list[i] = value; } void print(int list[], int size) { cout << "[ "; for(int i=0; i<size; i++) cout << list[i] << " "; cout << "]" << endl; }
30
Delete the first element
// for deleting the first element of the array int* deleteFirst(int list[], int& size){ int* newList; if (size>1) { newList = new int[size-1]; // make new array // copy and delete old array for(int i=0; i<size-1; i++) newList[i] = list[i+1]; delete[] list; size--; return newList; } else … If not empty!!!, cannot delete an empty list.
31
Remark: deleteFirst(A,n) if we define as a void type function:
Instead of A = deleteFirst(A,n) we can also just do deleteFirst(A,n) if we define as a void type function: void deleteFirst(int*& A, int& size) { … A = newList; } For a static array, A does not change, but the content of A changed. For a dynamic array, A changed!
32
Adding Elements // for adding a new element to end of array
int* addEnd(int list[], int& size, int value){ int* newList; newList = new int [size+1]; // make new array if(size){ // copy and delete old array for(int i=0; i<size; i++) newList[i] = list[i]; delete[] list; } newList[size] = value; size++; return newList; Not empty list
33
Add at the beginning: // for adding a new element at the beginning of the array int* addHead(int list[], int& size, int value){ int* newList; newList = new int [size+1]; // make new array if(size){ // copy and delete old array for(int i=0; i<size; i++) newList[i+1] = list[i]; delete[] list; } newList[0] = value; size++; return newList;
34
Search and delete … // for adding a new element to end of array
int* delete(int list[], int& size, int value){ Search the element in the array and get the position Shift all elements after it towards this position … }
35
pointers dynamic objects dynamic arrays linked list
list : static array dynamic array linked list
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.