More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture.

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

Linked Lists in C and C++ CS-2303, C-Term Linked Lists in C and C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
Linked Lists CS-212 Dick Steflik. Linked Lists A sequential collection of information Can be unordered; i.e. in no specific order Can be ordered; may.
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
CHP-5 LinkedList.
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Review Learn about linked lists
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Binary TreesCS-2303, C-Term Binary Trees (and Big “O” notation) CS-2303 System Programming Concepts (Slides include materials from The C Programming.
Linked Lists in C and C++ CS-2303, C-Term Linked Lists in C and C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
Linked Lists in C and C++ By Ravi Prakash PGT(CS).
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Lists and TreesCS-2301 D-term Data Structures — Lists and Trees CS-2301 System Programming D-term 2009 (Slides include materials from The C Programming.
Lists and Trees (continued) CS-2301, B-Term Lists and Trees (continued) CS-2301, System Programming for Non-Majors (Slides include materials from.
More on Data Structures in C CS-2301 B-term More on Lists and Trees Introduction to Hash Tables CS-2301, System Programming for Non-majors (Slides.
More on Data Structures in C CS-2301 D-term More on Data Structures in C CS-2301 System Programming D-term 2009 (Slides include materials from The.
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.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Data Structures, Lists, and Trees CS-2301 B-term Data Structures — Lists and Trees CS-2301, System Programming for Non-majors (Slides include materials.
Data Structures — Lists and Trees CS-2301, B-Term Data Structures — Lists and Trees CS-2301, System Programming for Non-Majors (Slides include materials.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title: Overview of Data Structure.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
1 Data Structures Lists and Trees. 2 Real-Life Computational Problems All about organizing data! –What shape the data should have to solve your problem.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
ECE 103 Engineering Programming Chapter 61 Abstract Data Types Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Linked List by Chapter 5 Linked List by
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
CNG 140 C Programming (Lecture set 12) Spring Chapter 13 Dynamic Data Structures.
 Array is a data structure were elements are stored in consecutive memory location.in the array once the memory is allocated.it cannot be extend any more.
CS-2852 Data Structures LECTURE 5 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
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.
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
CS32 Discussion Section 1B Week 3 TA: Hao Yu (Cody)
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
UNIT-II Topics to be covered Singly linked list Circular linked list
Data Structure and Algorithm: CIT231 Lecture 6: Linked Lists DeSiaMorewww.desiamore.com/ifm1.
CS162 - Topic #9 Lecture: Dynamic Data Structures –Deallocating all nodes in a LLL –Inserting nodes into sorted order Programming Assignment Questions.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
Data Structure By Amee Trivedi.
Introduction to Linked Lists
Lectures linked lists Chapter 6 of textbook
Linked Lists Chapter 6 Section 6.4 – 6.6
Data Structure Interview Question and Answers
ENERGY 211 / CME 211 Lecture 12 October 17, 2008.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Introduction to Linked Lists
Programming Assignment #4 Binary Trees in C++
Arrays and Linked Lists
Linked Lists in C and C++
Binary Trees (and Big “O” notation)
Introduction to C++ Linear Linked Lists
Stacks and Queues.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
BY PROF. IRSHAD AHMAD LONE.
Linked Lists.
Presentation transcript:

More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture note courtesy of Prof. David Bernstein,

Reminders Dynamically allocated memory is kept on the memory heap Dynamically allocated memory can't have a "name" it must be referred by pointers Declarations are used to statically allocate memory, the new operator is used to dynamically allocate memory

Pointing to Memory Allocated at Run Time

Using Memory Allocated at Run Time

Run Time Allocation of Arrays

Returning Memory to the Heap How Big is the Heap? –It can only contain as much physical memory as you have installed or as much virtual memory as your operating system can make available (if it supports virtual memory) Running Out of Memory: –Most applications request memory from the heap when they are running –It is possible to run out of memory (you may even have gotten a message like "Running Low On Virtual Memory") –So, it is important to return memory to the heap when you no longer need it

Returning Memory to the Heap

Dangling Pointers: –The delete operator does not delete the pointer, it takes the memory being pointed to and returns it to the heap –It does not even change the contents of the pointer –Since the memory being pointed to is no longer available (and may even be given to another application), such a pointer is said to be dangling

Returning Memory to the Heap Remember: –Return memory to the heap before undangling the pointer What's Wrong with the Following: –ptr = NULL; –delete ptr;

Returning Memory to the Heap What About Arrays? –You want to return all of the memory to the heap –So, a different form of the delete operator is needed –Also, the memory allocator must keep track of the size of the array

Returning Memory to the Heap

Memory Leak Memory leaks when it is allocated from the heap using the new operator but not returned to the heap using the delete operator

Memory Leak

Memory Efficiency 16

Linked Lists in C and C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie and from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel) 17

Definitions Linked List A data structure in which each element is dynamically allocated and in which elements point to each other to define a linear relationship Singly- or doubly-linked Stack, queue, circular list 18 Note: elements are usually the same type (but not always).

Linked List struct listItem { type payload; struct listItem *next; }; 19 payload next payload next payload next payload next Note: payload may be multiple members.

Linked List (continued) Items of list are usually same type Generally obtained from new operator Each item points to next item Last item points to null Need “ head ” to point to first item! “Payload” of item may be almost anything A single member or multiple members Any type of object whose size is known at compile time Including struct, union, char * or other pointers Also arrays of fixed size at compile time (see p. 214) 20

Usage of Linked Lists Not massive amounts of data Linear search is okay Sorting not necessary or sometimes not possible Need to add and delete data “on the fly” Even from middle of list Items often need to be added to or deleted from the “ends” 21

Linked List (continued) struct listItem { type payload; struct listItem *next; }; struct listItem *head; 22 payload next payload next payload next payload next

Adding an Item to a List struct listItem *p, *q; Add an item pointed to by q after item pointed to by p –Neither p nor q is NULL 23 payload next payload next payload next payload next payload next

Adding an Item to a List listItem *addAfter(listItem *p, listItem *q){ q -> next = p -> next; p -> next = q; return p; } 24 payload next payload next payload next payload next payload next

Adding an Item to a List listItem *addAfter(listItem *p, listItem *q){ q -> next = p -> next; p -> next = q; return p; } 25 payload next payload next payload next payload next payload next

Adding an Item to a List listItem *addAfter(listItem *p, listItem *q){ q -> next = p -> next; p -> next = q; return p; } 26 payload next payload next payload next payload next payload next Question: What to do if we cannot guarantee that p and q are non-NULL?

Adding an Item to a List (continued) listItem *addAfter(listItem *p, listItem *q){ if (p && q) { q -> next = p -> next; p -> next = q; } return p; } 27 payload next payload next payload next payload next payload next Note test for non-null p and q

What about Adding an Item before another Item? struct listItem *p; Add an item before item pointed to by p ( p != NULL ) 28 payload next payload next payload next payload next payload next

What about Adding an Item before another Item? Answer:– –Need to search list from beginning to find previous item –Add new item after previous item This is needed in PA#3 –Insert item after earlier event times and before later ones –Need to search the list 29

Doubly-Linked List struct listItem { type payload; listItem *prev; listItem *next; }; struct listItem *head, *tail; 30 prevnext payload prevnext payload prevnext payload prevnext payload In-class exercise:– how to add a new item q after a list item p

Other Kinds of List Structures Queue — FIFO (First In, First Out) Items added at end Items removed from beginning Stack — LIFO (Last In, First Out) Items added at beginning, removed from beginning Circular list Last item points to first item Head may point to first or last item Items added to end, removed from beginning 31

Circular List listItem *addAfter (listItem *p, listItem *tail){ if (p && tail) { p -> next = tail -> next; tail = p; } else if (p) { tail p -> next = p; } return tail; } 32 payload next payload next payload next payload next struct listItem *tail; Optional:– struct listItem *head;