Download presentation
Presentation is loading. Please wait.
1
Dynamic Objects
2
COMP104 Dynamic Objects / Slide 2 Memory Management * Static Memory Allocation n Memory is allocated at compiling time * Dynamic Memory n Memory is allocated at running time { int a[200]; … } { int n; cin >> n; a[n]??? }
3
COMP104 Dynamic Objects / Slide 3 Conceptual View of Memory (DYNAMIC MEMORY)
4
COMP104 Dynamic Objects / Slide 4 Static vs. Dynamic Objects * Static object n Memory is acquired automatically n Memory is returned automatically when object goes out of scope * Dynamic object n Memory is acquired by program with an allocation request new operation n Dynamic objects can exist beyond the function in which they were allocated n Object memory is returned by a deallocation request delete operation
5
COMP104 Dynamic Objects / Slide 5 Memory Allocation { int a[200]; … } int* ptr; ptr = new int[200]; … delete [] ptr; new delete
6
COMP104 Dynamic Objects / Slide 6 Creating a variable (object) Syntax ptr = new SomeType; where ptr is a pointer of type SomeType p Uninitialized int variable Example int* p = new int; ‘new’ does two things: 1. Create a variable of given type 2. Point the pointer p to the variable
7
COMP104 Dynamic Objects / Slide 7 Abracadabra!
8
COMP104 Dynamic Objects / Slide 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; p 10 ‘delete’ does two things: 1. Return the pointed object to the system 2. Point the pointer p to NULL
9
COMP104 Dynamic Objects / Slide 9 Dynamic Arrays (a collection of variables) Syntax for allocation: p = new SomeType[Expression]; n 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
COMP104 Dynamic Objects / Slide 10 Example Dynamic Memory Allocation 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
COMP104 Dynamic Objects / Slide 11 Freeing (or deleting) Memory
12
COMP104 Dynamic Objects / Slide 12 Allocation Example Need an array of unknown size 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... } int grades[n];
13
COMP104 Dynamic Objects / Slide 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
COMP104 Dynamic Objects / Slide 14 Memory Leak Problem int* A = new int[5]; for(int i=0; i<5; i++) A[i] = i; A = new int[5]; A 0 1 2 3 4 2
15
COMP104 Dynamic Objects / Slide 15 Static variables (objects)Dynamic variables (objects) A (direct) named memory locationA static part (pointer) + (indirect) nameless memory location (dynamic part) int a; a = 20; int* pa; pa = new int; *pa = 20; 20 a pa static dynamic Summary
16
COMP104 Dynamic Objects / Slide 16 int* p = new int; *p = 10; delete p; p 10 p int* p = new int[100]; for (i=1;i<100,i++) p[i] = 10; delete[] p; Simple dynamic variable Dynamic array ‘delete p’ is not sufficient for an array!!! ‘delete’ two actions: 1. Return the object pointed to 2. Point the pointer p to NULL 10
17
COMP104 Dynamic Objects / Slide 17 Good practice * Logically, Initialize a pointer with NULL * Always check a pointer before usage n if (p != NULL) *p
18
COMP104 Dynamic Objects / Slide 18 n=100; int* p = new int[n]; N=150; P[130]??? Bad examples { 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
COMP104 Dynamic Objects / Slide 19 A simple ‘list’ example * Concept of a list, e.g. a list of integers * A list is a linear sequence of objects n Print out info n Empty test n Search an element n Insertion (at head, at end, any position) n Deletion n … * implemented n by a static array (over-sized if necessary) int list[1000]; int size; n by a dynamic array int list[size]; int size; n by a linked list and more …
20
COMP104 Dynamic Objects / Slide 20 int main() { int A[1000]; int n; cin >> n; initialize(A, n, 0); print(A, n); A = addEnd(A,n,5); print(A, n); A = addHead(A,n,5); print(A, n); A = deleteFirst(A,n); print(A, n); selectionSort(A, n); print(A, n); } How to use a list? Static array! const int DIM=1000; int main() { int A[DIM]; int n; cin >> n; … }
21
COMP104 Dynamic Objects / Slide 21 Initialize void initialize(int list[], int size, int value){ for(int i=0; i<size; i++) list[i] = value; }
22
COMP104 Dynamic Objects / Slide 22 void print(int list[], int size) { cout << "[ "; for(int i=0; i<size; i++) cout << list[i] << " "; cout << "]" << endl; } Print out a list
23
COMP104 Dynamic Objects / Slide 23 Delete the first element int* deleteFirst(int list[], int& size){ for(int i=0; i<size-1; i++) list[i] = list[i+1]; size--; return list; } // 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--; } A = deleteFirst(A,n) deleteFirst(A,n) Pointer type!
24
COMP104 Dynamic Objects / Slide 24 Adding Elements int* addEnd(int list[], int& size, int value){ if ((size>0) && (size < DIM)) { list[size] = value; size++; } return list; } // for adding a new element to end of array void addEnd(int list[], int& size, int value){ if (size < DIM) { list[size] = value; size++; } A = addEnd(A,n,v) addEnd(A,n,v) If not full!
25
COMP104 Dynamic Objects / Slide 25 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 … } Add at the beginning: // 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; } A = addHead(A,n,v) addHead(A,n,v)
26
COMP104 Dynamic Objects / Slide 26 Using a dynamic array int main() { cout << "Enter list size: "; int n; cin >> n; int* A = new int[n]; … }
27
COMP104 Dynamic Objects / Slide 27 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); print(A, n); A = addHead(A,n,5); print(A, n); A = deleteFirst(A,n); print(A, n); selectionSort(A, n); print(A, n); delete[] A; } How to use a list? int A[1000]; int n; cin >> n;
28
COMP104 Dynamic Objects / Slide 28 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; }
29
COMP104 Dynamic Objects / Slide 29 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.
30
COMP104 Dynamic Objects / Slide 30 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; } Remark: For a static array, A does not change, but the content of A changed. For a dynamic array, A changed!
31
COMP104 Dynamic Objects / Slide 31 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
32
COMP104 Dynamic Objects / Slide 32 // 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; } Add at the beginning:
33
COMP104 Dynamic Objects / Slide 33 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 … }
34
COMP104 Dynamic Objects / Slide 34 pointers dynamic objects dynamic arrays linked list list static array dynamic array linked list
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.