A First Book of ANSI C

Similar documents
Dynamic Memory: Alignment and Fragmentation

BBM 201 DATA STRUCTURES

Dynamic Data Structures. CSCI 112: Programming in C

CP2 Revision. theme: dynamic datatypes & data structures

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

Dynamic Memory Management. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

dynamic memory allocation

Heap Arrays and Linked Lists. Steven R. Bagley

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Allocating Space

Memory Management. CSC215 Lecture

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

CSCI 171 Chapter Outlines

In Java we have the keyword null, which is the value of an uninitialized reference type

Jump Tables A jump table is essentially a list of places in the code to jump to. This can be thought of in C as an array of function pointers. In Asse

CS201 Some Important Definitions

Programs in memory. The layout of memory is roughly:

Common Misunderstandings from Exam 1 Material

Cpt S 122 Data Structures. Data Structures

Advanced Pointer & Data Storage

Memory Organization. The machine code and data associated with it are in the code segment

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

Fall 2018 Discussion 2: September 3, 2018

(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University

19-Nov CSCI 2132 Software Development Lecture 29: Linked Lists. Faculty of Computer Science, Dalhousie University Heap (Free Store)

CS 11 C track: lecture 5

Mid-term Exam. Fall Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

Heap Arrays. Steven R. Bagley

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

CS 2113 Software Engineering

Chapter 14. Dynamic Data Structures. Instructor: Öğr. Gör. Okan Vardarlı. Copyright 2004 Pearson Addison-Wesley. All rights reserved.

Lists (Section 5) Lists, linked lists Implementation of lists in C Other list structures List implementation of stacks, queues, priority queues

Binghamton University. CS-211 Fall Dynamic Memory

Base Component. Chapter 1. *Memory Management. Memory management Errors Exception Handling Messages Debug code Options Basic data types Multithreading

User created Heap Management in C on AIX

Section 2: Processes

CSCI 2212: Intermediate Programming / C Storage Class and Dynamic Allocation

Singly linked lists in C.

Dynamically Allocated Memory in C

G Programming Languages - Fall 2012

MODULE 5: Pointers, Preprocessor Directives and Data Structures

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

ECE 2400 Computer Systems Programming Fall 2018 Topic 6: C Dynamic Allocation

Dynamic Allocation in C

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Dynamic Allocation in C

Last week. Data on the stack is allocated automatically when we do a function call, and removed when we return

IBS Software Services Technical Interview Questions. Q1. What is the difference between declaration and definition?

Data Structure Series

NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313.

COMP26120: Linked List in C (2018/19) Lucas Cordeiro

(1-2) Introduction to C Data Structures & Abstract Data Types. Instructor - Andrew S. O Fallon CptS 122 (June 6, 2018) Washington State University

Dynamic Memory Allocation

Review! Lecture 5 C Memory Management !

CS61C : Machine Structures

Memory and C/C++ modules

C++ PROGRAMMING LANGUAGE: DYNAMIC MEMORY ALLOCATION AND EXCEPTION IN C++. CAAM 519, CHAPTER 15

Creating a String Data Type in C

Chapter 3: Process-Concept. Operating System Concepts 8 th Edition,

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

Kurt Schmidt. October 30, 2018

C Programming Basics II

UNIT IV 4 LINKED LIST

Mid-term Exam. Fall Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

Data Structures Lecture 2 : Part 1: Programming Methodologies Part 2: Memory Management

1 Dynamic Memory continued: Memory Leaks

Memory Allocation. General Questions

Dynamic Allocation of Memory

CS 33. Intro to Storage Allocation. CS33 Intro to Computer Systems XXVI 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

2/9/18. Secure Coding. CYSE 411/AIT681 Secure Software Engineering. Agenda. Dynamic Memory Interface. Dynamic Memory Interface

Reminder: compiling & linking

Process. Heechul Yun. Disclaimer: some slides are adopted from the book authors slides with permission

CS201- Introduction to Programming Current Quizzes

CSC 1600 Memory Layout for Unix Processes"

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0)

MPATE-GE 2618: C Programming for Music Technology. Syllabus

struct Properties C struct Types

DAY 3. CS3600, Northeastern University. Alan Mislove

The C++ Object Lifecycle. EECS 211 Winter 2019

Memory (Stack and Heap)

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008.

Hacettepe University Department Of Computer Engineering BBM 103 Introduction to Programming Experiment 5

Stack memory - "scratch pad" memory that is used by automatic variables.

CS240: Programming in C

Writing Functions in C

DOWNLOAD PDF LINKED LIST PROGRAMS IN DATA STRUCTURE

CS 241 Honors Memory

Here's how you declare a function that returns a pointer to a character:

CS61C : Machine Structures

C++ for Java Programmers

CSCI 4210 Operating Systems CSCI 6140 Computer Operating Systems Homework 3 (document version 1.2) Multi-threading in C using Pthreads

A First Book of ANSI C Fourth Edition. Chapter 12 Structures

Classes in C++98 and C++11

LAB 6 (2017 June 22/27) Array of pointers. Dynamic memory allocation.

Spring 2016, Malloc Lab: Writing Dynamic Memory Allocator

Secure Coding in C and C++

Transcription:

A First Book of ANSI C Fourth Edition Chapter 13 Dynamic Data Structures

Objectives Introduction to Linked Lists Dynamic Memory Allocation Dynamically Linked Lists Common Programming and Compiler Errors A First Book of ANSI C, Fourth Edition 2

Introduction Dynamic memory allocation: an alternative to fixed memory allocation in which memory space grows or diminishes during program execution Dynamic memory allocation makes it unnecessary to reserve a fixed amount of memory for a scalar, array, or structure variable in advance - Also known as run-time allocation - Requests are made for allocation and release of memory space while the program is running A First Book of ANSI C, Fourth Edition 3

Introduction to Linked Lists An array of structures can be used to insert and delete ordered structures, but this is not an efficient use of memory - Better alternative: a linked list Linked list: set of structures, each containing at least one member whose value is the address of the next logically ordered structure in the list - Also known as self-referencing structures A First Book of ANSI C, Fourth Edition 4

Introduction to Linked Lists (continued) A First Book of ANSI C, Fourth Edition 5

Introduction to Linked Lists (continued) A First Book of ANSI C, Fourth Edition 6

Introduction to Linked Lists (continued) A NULL acts as a sentinel or flag to indicate when the last structure has been processed In addition to an end-of-list sentinel value, we must provide a special pointer for storing the address of the first structure in the list A First Book of ANSI C, Fourth Edition 7

Introduction to Linked Lists (continued) A First Book of ANSI C, Fourth Edition 8

Introduction to Linked Lists (continued) A First Book of ANSI C, Fourth Edition 9

Introduction to Linked Lists (continued) A structure can contain any data type, including a pointer. A pointer member of a structure is used like any other pointer variable. A First Book of ANSI C, Fourth Edition 10

Introduction to Linked Lists (continued) A First Book of ANSI C, Fourth Edition is evaluated as (t1.nextaddr)->name it can be replaced by (*t1.nextaddr).name 11

Introduction to Linked Lists (continued) A First Book of ANSI C, Fourth Edition 12

Introduction to Linked Lists (continued) Disadvantage: exactly three structures are defined in main() by name, and storage for them is reserved at compile time A First Book of ANSI C, Fourth Edition 13

Introduction to Linked Lists (continued) can be replaced by while(!contents) A First Book of ANSI C, Fourth Edition 14

Dynamic Memory Allocation A First Book of ANSI C, Fourth Edition 15

Dynamic Memory Allocation (continued) The malloc() and calloc() functions can frequently be used interchangeably - The advantage of calloc() is that it initializes all newly allocated numeric memory to 0 and character allocated memory to NULL - We use malloc() because it is the more general purpose of the two functions - malloc(10*sizeof(char)) or calloc(10,sizeof(char)) requests enough memory to store 10 characters The space allocated by malloc() comes from the computer's heap A First Book of ANSI C, Fourth Edition 16

Dynamic Memory Allocation (continued) A First Book of ANSI C, Fourth Edition 17

Dynamic Memory Allocation (continued) malloc() is more typically used for dynamically allocating memory for structures struct OfficeInfo *Off; /* request space for one structure */ Off = (struct OfficeInfo *) malloc(sizeof(struct OfficeInfo)); /* check that space was allocated */ if (Off == (struct OfficeInfo*) NULL) { } printf("\nallocation failed\n"); exit(1); A First Book of ANSI C, Fourth Edition 18

Dynamically Linked Lists In a dynamically linked list, this capability is extended to permit adding or deleting a structure from anywhere within the list Such a capability is extremely useful when structures must be kept within a specified order A First Book of ANSI C, Fourth Edition 19

INSERT and DELETE Key field A First Book of ANSI C, Fourth Edition 20

INSERT and DELETE (continued) INSERT (add a new structure into a linked list) Dynamically allocate space for a new structure If no structures exist in the list Set the address field of the new structure to a NULL Set address in the first structure pointer to address of newly created structure Else /* we are working with an existing list */ Locate where this new structure should be placed If this structure should be the new first structure in the list Copy current contents of first structure pointer into address field of newly created structure Set address in the first structure pointer to address of newly created structure Else Copy the address in the prior structure's address member into the address field of the newly created structure Set address of prior structure's address member to address of newly created structure EndIf EndIf A First Book of ANSI C, Fourth Edition 21

INSERT and DELETE (continued) A First Book of ANSI C, Fourth Edition 22

INSERT and DELETE (continued) LINEAR LOCATION for INSERTING a NEW STRUCTURE If the key field of the new structure is less than the first structure's key field the new structure should be the new first structure Else While there are still more structures in the list Compare the new structure's key value to each structure key Stop the comparison when the new structure key either falls between two existing structures or belongs at the end of the existing list EndWhile EndIf A First Book of ANSI C, Fourth Edition 23

INSERT and DELETE (continued) See Slide 27 See Slide 27 A First Book of ANSI C, Fourth Edition 24

INSERT and DELETE (continued) A First Book of ANSI C, Fourth Edition 25

INSERT and DELETE (continued) A First Book of ANSI C, Fourth Edition 26

INSERT and DELETE (continued)...(code for insert() and locate() goes here) A First Book of ANSI C, Fourth Edition 27

INSERT and DELETE (continued) The following sample run shows the results of these tests: Enter as many names as you wish, one per line To stop entering names, enter a single x Enter a name: Binstock Enter a name: Arnold Enter a name: Duberry Enter a name: Carter Enter a name: x The names currently in the list, in alphabetical order, are: Arnold Binstock Carter Duberry A First Book of ANSI C, Fourth Edition 28

Common Programming Errors Not checking the return codes provided by malloc() and realloc() Not correctly updating all relevant pointer addresses when adding or removing structures from dynamically linked lists Forgetting to free previously allocated memory space when the space is no longer needed A First Book of ANSI C, Fourth Edition 29

Common Compiler Errors A First Book of ANSI C, Fourth Edition 30

Common Compiler Errors (continued) A First Book of ANSI C, Fourth Edition 31

Summary An alternative to fixed memory allocation for variables at compile time is the dynamic allocation of memory at run time malloc() reserves a requested number of bytes and returns a pointer to the first reserved byte realloc() operates in a similar fashion as malloc() except it is used to expand or contract an existing allocated space free() is used to deallocate previously allocated memory space A dynamically linked list consists of structures that can be added or removed from any position in the list. A First Book of ANSI C, Fourth Edition 32