EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019

Slides:



Advertisements
Similar presentations
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Advertisements

DYNAMIC MEMORY MANAGEMENT. int x; When the source code containing this statement is compiled and linked, an executable file is generated. When the executable.
Informática II Prof. Dr. Gustavo Patiño MJ
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.
Dynamic Objects. COMP104 Dynamic Objects / Slide 2 Memory Management * Static Memory Allocation n Memory is allocated at compilation time * Dynamic Memory.
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.
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 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Chapter 9 Pointers Fall 2005 Csc 125 Introduction to C++
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Dynamic Memory Allocation 9.8.
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.
Stack and Heap Memory Stack resident variables include:
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
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 and Pointers Lecture 4.
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.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 22: Pointers.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
1  Lecture 12 – Pointer FTMK, UTeM – Sem /2014.
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.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Arrays
Chapter 9: Pointers.
Introduction to Programming
ECE Application Programming
Pointers Revisited What is variable address, name, value?
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Pointers and References
Dynamic Memory Allocation
CSC 253 Lecture 8.
An Introduction to Pointers
CSC 253 Lecture 8.
Pointers and dynamic objects
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
Dynamic Memory A whole heap of fun….
Dynamic Memory Allocation
Chapter 15 Pointers, Dynamic Data, and Reference Types
EECE.2160 ECE Application Programming
Dynamic Memory A whole heap of fun….
EECE.2160 ECE Application Programming
ECE Application Programming
Dynamic Memory A whole heap of fun….
Dynamic Memory.
EECE.2160 ECE Application Programming
Instructor: Dr. Michael Geiger Spring 2019 Lecture 29: Linked queues
Standard Version of Starting Out with C++, 4th Edition
Dynamic allocation (continued)
Dynamic Memory Allocation
DYNAMIC MEMORY MANAGEMENT
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Pointers and dynamic objects
Pointers and References
Pointers and References
Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Pointers, Dynamic Data, and Reference Types
EECE.2160 ECE Application Programming
Dynamic Objects.
Presentation transcript:

EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019 Lecture 18: Dynamic allocation

Announcements/reminders Program 2 due 3/21 Exam 2: Monday, 4/1, 3-5 PM, Ball 214 Use same poll as before to request alt. exam (I’ll repost link) Today’s lecture Dynamic allocation 4/26/2019 Data Structures: Lecture 18

Data Structures: Lecture 18 Refresher on pointers Allocators (malloc, new) return pointer to allocated space Pointer: address of another object Can get address of existing object using & Can get value of existing pointer using * Pointer declaration: <base type>* <pointer name> Base type determines how reference is interpreted Be careful when declaring multiple pointers Be sure to initialize pointer before use 4/26/2019 Data Structures: Lecture 18

Refresher on pointers, pt. 2 Can assign pointers to one another; e.g. int x, *xp, *ip; xp = &x; ip = xp; Array/pointer duality Array name is pointer to first element Can dereference array name (*arr) Can treat pointers like arrays (ptr[i]) 4/26/2019 Data Structures: Lecture 18

Dynamic memory allocation Up until now, allocated memory statically Assumed we knew data size at compile time What if data size is input-dependent and unknown until run time? In C, dynamic memory allocation handled through malloc and free In C++, we use new and delete Allocator (new) returns pointer to allocated space 4/26/2019 Data Structures: Lecture 18

Data Structures: Lecture 18 Allocation with new In C, malloc()allocates space on heap malloc(sizeof(int)) allocates space for 1 integer malloc(20*sizeof(int)) allocates an array of 20 integers Both calls return pointer to first byte of element In C++, we use new new int allocates space for 1 integer new int[20] allocates an array of 20 integers As with malloc(), new returns pointer to first byte Directly initialize with value in parentheses e.g. new int(3) 4/26/2019 Data Structures: Lecture 18

Data Structures: Lecture 18 Example int *iPtr; iPtr = new int; // 4 bytes are allocated // iPtr points to 1st byte double *dPtr; dPtr = new double[20]; // 160 bytes allocated // dPtr points to 1st byte heap iPtr ? … 4 bytes allocated for single int variable dPtr 160 bytes allocated for 20 double variables 4/26/2019 Data Structures: Lecture 18

Dynamic allocation example int main() { int *iPtr, *jPtr, i; iPtr = new int; jPtr = new int(3); double *dPtr; dPtr = new double[6]; *iPtr = 7; cout << *iPtr << ',' << *jPtr << endl; for(i=0; i<6; i++) dPtr[i] = 5; cout << (*dPtr)++ << ' '; cout << endl; cout << dPtr[i] << ' '; return 0; } OUTPUT 7, 3 5 6 7 8 9 10 11 5 5 5 5 5 Why? 4/26/2019 Data Structures: Lecture 18

Dynamically allocated objects May want to dynamically allocate objects Ex. array of Points where size is unknown at compile time Point *pointArray; int numPoints; cin >> numPoints; pointArray = new Point[numPoints]; Ex. linked list data structure—to add an element to the list, must allocate new element class linkedList { private: int elem; linkedList *next; public: linkedList(); linkedList(int val); void addElement(int i); ... } void linkedList::addElement(int i) { next = new linkedList(i); 4/26/2019 Data Structures: Lecture 18

Referencing objects through pointers Recall: use dot operator (.) to reference members of object, e.g: Point p1; p1.setX(2); With pointers, use -> linkedList *list1 = new linkedList; list1->addElement(2); 4/26/2019 Data Structures: Lecture 18

Deallocation with delete Space allocated using new should be freed In C, we used free() In C++, we use delete You should only use delete to free memory allocated by new Any other use will result in an error 4/26/2019 Data Structures: Lecture 18

Data Structures: Lecture 18 delete example int *ptr; ptr = new int (100); ptr 100 delete ptr; //free the memory ptr ? delete frees space on heap ... ... but ptr still points to same address! Solution: assign freed pointers to NULL: ptr = NULL; 4/26/2019 Data Structures: Lecture 18

Example: delete with arrays double *dptr; const int SIZE = 10; dptr = new double[SIZE]; //80 bytes for(int i=0; i<SIZE; ++i) cin >> dptr[i]; fun1(dptr, SIZE); // pass array to fun1 delete [] dptr; //free all 10 elements dptr = NULL; 4/26/2019 Data Structures: Lecture 18

Data Structures: Lecture 18 Final notes Next time Stacks Reminders: Program 2 due 3/21 Exam 2: Monday, 4/1, 3-5 PM, Ball 214 Use same poll as before to request alt. exam (I’ll repost link) 4/26/2019 Data Structures: Lecture 18