Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dynamic allocation (continued)

Similar presentations


Presentation on theme: "Dynamic allocation (continued)"— Presentation transcript:

1 Dynamic allocation (continued)
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019 Lecture 19: Dynamic allocation (continued) Stacks

2 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 Review: dynamic allocation Linked data structures Stacks 4/28/2019 Data Structures: Lecture 19

3 Review: Dynamic allocation with new
Use new to dynamically allocate space new int allocates space for 1 integer new int[20] allocates an array of 20 integers new “returns” pointer to first byte Directly initialize with value in parentheses e.g. new int(3) Pointer must hold address generated by new int *p1 = new int(3); // p1  single int = 3 int *p2 = new int[20]; // p2  array of // ints 4/28/2019 Data Structures: Lecture 19

4 Review: 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/28/2019 Data Structures: Lecture 19

5 Review: Referencing objects thru 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/28/2019 Data Structures: Lecture 19

6 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/28/2019 Data Structures: Lecture 19

7 Data Structures: Lecture 19
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/28/2019 Data Structures: Lecture 19

8 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/28/2019 Data Structures: Lecture 19

9 Data Structures: Lecture 19
List ADT Common problem in program: store collection—or list—of things Grocery list, grade list, list of students, etc. Common properties Homogeneous (elements all have same type) Finite length (number of elements) Length could be 1 or even 0 Elements arranged sequentially Well-defined first and last element All except last have unique successor All except first have unique predecessor 4/28/2019 Data Structures: Lecture 19

10 Data Structures: Lecture 19
List ADT (continued) A sequence of a finite number of data items, all of the same type Basic operations Construction: create empty list Empty: check if the list is empty Insert: add an item to the list Delete: remove an item from the list Traverse: go through part or all of list, accessing and processing elements in order Types of traversal include search, output (to screen or file), copy, rearrange (usually sort) 4/28/2019 Data Structures: Lecture 19

11 List-based data structures
List ADT used for several data structures Differences between them Where can data be inserted? Where can data be removed? What determines ordering of items? List-based data structures Stacks: insert/remove at “top” (LIFO) Queue: insert at back, remove at front (FIFO) Linked list: insert/remove anywhere, can order as needed 4/28/2019 Data Structures: Lecture 19

12 Data Structures: Lecture 19
Array-based lists Implementing ADT Define necessary data members Define methods described in ADT design Common list implementation: array Built-in type in most languages Sequential memory storage Straightforward algorithms For (some) flexibility, dynamically allocate array Define max size of list at construction Dynamic allocation does cause some issues with construction (to be seen) 4/28/2019 Data Structures: Lecture 19

13 Data Structures: Lecture 19
Stacks Consider the following applications Run-time memory management On function call, create space for local data (stack frame or activation record) On return, clear frame for current function Traversing maze Follow path until dead end Backtrack to last intersection and choose new path In all cases Need ability to access only most recent data If necessary, will only remove most recent data Appropriate data structure: stack 4/28/2019 Data Structures: Lecture 19

14 Data Structures: Lecture 19
Stack ADT Stack is last-in, first-out (LIFO) data structure Definition Ordered collection of data items Can only be accessed at one end (top of stack) Operations Construction (start with empty stack) Check if stack is empty Push: add data to the top of the stack Pop: remove data from the top of the stack Read item at top of stack 4/28/2019 Data Structures: Lecture 19

15 Data Structures: Lecture 19
Final notes Next time: more on stacks Details of array-based implementation Dynamic allocation, constructors, and destructors Operator overloading 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/28/2019 Data Structures: Lecture 19


Download ppt "Dynamic allocation (continued)"

Similar presentations


Ads by Google