Dynamic Objects.

Slides:



Advertisements
Similar presentations
COMP171 Data Structures and Algorithm Tutorial 2 TA: M.Y.Chan.
Advertisements

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.
Dynamic Objects. COMP104 Dynamic Objects / Slide 2 Memory Management * Static Memory Allocation n Memory is allocated at compiling time * Dynamic Memory.
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.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
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.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
List, (dynamic) linked list Let’s first forget about ‘classes’, but only a dynamic list. We make lists with ‘classes’ afterwards.
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.
HKUST Summer Programming Course 2008
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
February 11, 2005 More Pointers Dynamic Memory Allocation.
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
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Pointers and Dynamic Objects Mechanisms for developing flexible list representations JPC and JWD © 2002 McGraw-Hill, Inc.
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.
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
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.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
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.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
CSC Pointers Powerful feature of the C++ language One of the most difficult to master Essential for construction of interesting data structures.
Pointers and Arrays Dynamic Variables and Arrays.
Pointers CSC1201: Programming Language 2. Topics Pointers ▫Memory addresses ▫Declaration ▫Dereferencing a pointer ▫Pointers to pointer.
Pointers and Dynamic Memory Allocation
Overview 4 major memory segments Key differences from Java stack
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Introduction to Programming
CSC113: Computer Programming (Theory = 03, Lab = 01)
Pointer Data Type and Pointer Variables II
Dynamic Memory Allocation Reference Variables
Dynamic Memory Allocation
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
as an abstract data structure and
Overview 4 major memory segments Key differences from Java stack
Pointers and dynamic objects
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
Programming II Pointers and arrays by aalosaimi.
Dynamic Memory A whole heap of fun….
Chapter 15 Pointers, Dynamic Data, and Reference Types
Dynamic Memory A whole heap of fun….
Pointers.
7 Arrays.
Arrays an array of 5 ints is filled with 3,2,4,1,7
Dynamic Objects.
Dynamic Memory A whole heap of fun….
Dynamic Memory.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Pointers and dynamic objects
COP 3330 Object-oriented Programming in C++
Dynamic allocation (continued)
Data Structures and Algorithms Memory allocation and Dynamic Array
Pointers and dynamic objects
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

Dynamic Objects

Memory Management Static Memory Allocation Dynamic Memory Memory is allocated at compilation time Dynamic Memory Memory is allocated at running time

Static vs. Dynamic Objects 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 object (variables as declared in function calls) Memory is acquired automatically Memory is returned automatically when object goes out of scope

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

Object (variable) creation: New Syntax ptr = new SomeType; where ptr is a pointer of type SomeType Example int* p = new int; p Uninitialized int variable

Object (variable) destruction: Delete 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

Array of New: dynamic arrays Syntax 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 Because of the flexible pointer syntax, P can be considered to be an array

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]; new p new p new p = new int[n]; p

Memory Allocation Example Want an array of unknown size #include <iostream> using namespace std; void 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

Freeing (or deleting) Memory

A Simple Dynamic List Example cout << "Enter list size: "; int n; cin >> n; int *A = new int[n]; if(n<=0){ cout << "bad size" << endl; return 0; } initialize(A, n, 0); // initialize the array A with value 0 print(A, n); A = addElement(A,n,5); //add an element of value 5 at the end of A A = deleteFirst(A,n); // delete the first element from A selectionSort(A, n); // sort the array (not shown) delete [] A;

Initialize void initialize(int list[], int size, int value){ for(int i=0; i<size; i++) list[i] = value; }

print() void print(int list[], int size) { cout << "[ "; for(int i=0; i<size; i++) cout << list[i] << " "; cout << "]" << endl; } Remember in C++, array parameters are always passed by reference. That is, void print(int list[], int size) {…} is the same as void print(int * list , int size) {…} Note: no & used here, so, the pointer itself is passed by value

Adding Elements // for adding a new element to end of array int* addElement(int list[], int& size, int value){ int* newList = new int [size+1]; // make new array if(newList==0){ cout << "Memory allocation error for addElement!" << endl; exit(-1); } for(int i=0; i<size; i++) newList[i] = list[i]; if(size) delete [] list; newList[size] = value; size++; return newList;

Delete the first element // for deleting the first element of the array int* deleteFirst(int list[], int& size){ if(size <= 1){ if( size) delete list; size = 0; return NULL; } int* newList = new int [size-1]; // make new array if(newList==0){ cout << "Memory allocation error for deleteFirst!" << endl; exit(-1); for(int i=0; i<size-1; i++) // copy and delete old array newList[i] = list[i+1]; delete [] list; size--; return newList;

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!

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

A Dynamic 2D Array 32 18 24 12 42 14 19 16 11 13 22 table table[0] A dynamic array is an array of pointers to save space when not all rows of the array are full. int **table; 32 18 24 12 42 14 19 16 11 13 22 table = new int*[6]; … table[0] = new int[4]; table[1] = new int[7]; table[2] = new int[1]; table[3] = new int[3]; table[4] = new int[2]; table[5] = NULL; table[0] table[1] table[2] table[3] table[4] table[5] table

Memory Allocation int **table; table = new int*[6]; table[0][0] = 1; table[0][1] = 2; table[0][2] = 3; table[1][0] = 4; table[2][0] = 5; table[2][1] = 6; table[2][2] = 7; table[2][3] = 8; table[2][4] = 9; table[4][0] = 10; table[4][1] = 11; cout << table[2][5] << endl;

Memory Deallocation Memory leak is a serious bug! Each row must be deleted individually Be careful to delete each row before deleting the table pointer. for(int i=0; i<6; i++) delete [ ] table[i]; delete [ ] table;

Create a matrix of any dimensions, m by n: Put it into a function: int m, n; cin >> m >> n >> endl; int** mat; mat = new int*[m]; for (int i=0;i<m;i++) mat[i] = new int[n]; int m, n; cin >> m >> n >> endl; int** mat; mat = imatrix(m,n); … int** imatrix(nr, nc) { int** m; m = new int*[nr]; for (int i=0;i<nr;i++) m[i] = new int[nc]; return m; }