Dynamic Objects. COMP104 Dynamic Objects / Slide 2 Memory Management * Static Memory Allocation n Memory is allocated at compiling time * Dynamic Memory.

Slides:



Advertisements
Similar presentations
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Advertisements

COMP171 Data Structures and Algorithm Tutorial 2 TA: M.Y.Chan.
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
List as an Abstract Data Type. struct Node{ public: int data; Node* next; }; typedef Node* Nodeptr; class List { public: List(); // constructor List(const.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Dynamic Objects. COMP104 Lecture 31 / Slide 2 Static verses Dynamic Objects * Static object n Memory is acquired automatically  int A[10]; n Memory is.
Dynamic Objects. COMP104 Dynamic Objects / Slide 2 Memory Management * Static Memory Allocation n Memory is allocated at compilation time * Dynamic Memory.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Pointers and Dynamic Objects Mechanisms for developing flexible list representations.
Pointers and dynamic objects COMP171 Fall Pointers and dynamic objects/ Slide 2 Topics * Pointers n Memory addresses n Declaration n Dereferencing.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Concept of lists and array implementations of lists.
List as an Abstract Data Type. struct Node{ public: int data; Node* next; }; typedef Node* Nodeptr; class list { public: list(); // constructor list(const.
COMP103 - Linked Lists (Part A)1 Chapter 17 Truly dynamic memory management.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
List, (dynamic) linked list Let’s first forget about ‘classes’, but only a dynamic list. We make lists with ‘classes’ afterwards.
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
Dynamic Objects II. COMP104 Lecture 31 / Slide 2 A Simple Dynamic List  An integer list: IntArray * Features n Can be passed by value & reference n Can.
Pointers. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program execution.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Pointer Data Type and Pointer Variables
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
C++ Pointers Copies from SEE C++ programming course and from Starting Out with C++: Early Objects, 8/E by Tony Gaddis, Judy Walters and Godfrey Muganda.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Chapter 5 – Dynamic Data Structure Par1: Abstract Data Type DATA STRUCTURES & ALGORITHMS Teacher: Nguyen Do Thai Nguyen
Pointers and Dynamic Objects Mechanisms for developing flexible list representations JPC and JWD © 2002 McGraw-Hill, Inc.
Pointers OVERVIEW.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Object-Oriented Programming in C++
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Dynamic Allocation Joe Meehean. Dynamic Allocation Memory for local objects is automatically created and reclaimed memory is created for it at beginning.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Review 1 List Data Structure List operations List Implementation Array Linked List.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.
Chapter 11 Mechanisms for developing flexible list representations Pointers and dynamic memory.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
CSC Pointers Powerful feature of the C++ language One of the most difficult to master Essential for construction of interesting data structures.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
Pointers CSC1201: Programming Language 2. Topics Pointers ▫Memory addresses ▫Declaration ▫Dereferencing a pointer ▫Pointers to pointer.
Linked lists.
Pointers Revisited What is variable address, name, value?
CSCE 210 Data Structures and Algorithms
Dynamic Memory Allocation
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 16-2 Linked Structures
as an abstract data structure and
Pointers and dynamic objects
Pointers.
Dynamic Objects.
Dynamic Memory.
Linked Lists.
Dynamic allocation (continued)
Data Structures and Algorithms Memory allocation and Dynamic Array
List as an Abstract Data Type
Pointers and dynamic objects
Linked lists.
Dynamic Objects.
Presentation transcript:

Dynamic Objects

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]??? }

COMP104 Dynamic Objects / Slide 3 Conceptual View of Memory (DYNAMIC MEMORY)

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

COMP104 Dynamic Objects / Slide 5 Memory Allocation { int a[200]; … } int* ptr; ptr = new int[200]; … delete [] ptr; new delete

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

COMP104 Dynamic Objects / Slide 7 Abracadabra!

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

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

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

COMP104 Dynamic Objects / Slide 11 Freeing (or deleting) Memory

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];

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!

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

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

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

COMP104 Dynamic Objects / Slide 17 Good practice * Logically, Initialize a pointer with NULL * Always check a pointer before usage n if (p != NULL) *p

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.

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 …

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; … }

COMP104 Dynamic Objects / Slide 21 Initialize void initialize(int list[], int size, int value){ for(int i=0; i<size; i++) list[i] = value; }

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

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!

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!

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)

COMP104 Dynamic Objects / Slide 26 Using a dynamic array int main() { cout << "Enter list size: "; int n; cin >> n; int* A = new int[n]; … }

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;

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; }

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.

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!

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

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:

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 … }

COMP104 Dynamic Objects / Slide 34 pointers  dynamic objects  dynamic arrays  linked list list  static array  dynamic array  linked list