1 / 35

Dynamic Objects

Understand dynamic and static memory allocation in programming. Learn how to manage memory, dynamically allocate objects, and avoid issues like dangling pointers and memory leaks.

normanh
Download Presentation

Dynamic Objects

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Dynamic Objects

  2. Memory Management • Static Memory Allocation • 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 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 Static vs. Dynamic Objects

  5. Memory Allocation new delete { int a[200]; … } int* ptr; ptr = new int[200]; … delete [] ptr;

  6. Uninitialized int variable p Creating a variable (object) Syntax ptr = new SomeType; where ptr is a pointer of type SomeType Example int* p = new int; ‘new’ does two things: • Create a variable of given type • Point the pointer p to the variable

  7. Abracadabra!

  8. 10 p 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

  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 • 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. 2 3 Memory Leak Problem int* A = new int[5]; for(int i=0; i<5; i++) A[i] = i; A = new int[5]; 1 • 2 A 0 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 dynamic static static

  16. 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 10 10 p p ‘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. 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? 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); print(A, n); A = addHead(A,n,5); print(A, n); A = deleteFirst(A,n); print(A, n); selectionSort(A, n); print(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--; } int* deleteFirst(int list[], int& size){ for(int i=0; i<size-1; i++) list[i] = list[i+1]; size--; return list; } deleteFirst(A,n) A = deleteFirst(A,n) Pointer type!

  25. Adding Elements // for adding a new element to end of array void addEnd(int list[], int& size, int value){ if (size < DIM) { list[size] = value; size++; } } int* addEnd(int list[], int& size, int value){ if ((size>0) && (size < DIM)) { list[size] = value; size++; } return list; } addEnd(A,n,v) If not full! A = addEnd(A,n,v)

  26. 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; } 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 … } addHead(A,n,v) 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 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; } 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: 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

More Related