CS 261: Data Structures. Dynamic Arrays. Introduction

Similar documents
CS 261. Dynamic Arrays Introduction by Tim Budd Ron Metoyer Sinisa Todorovic

CS 261: Data Structures. Dynamic Array Queue

CS 261. Dynamic Array Queue by Tim Budd Ron Metoyer Sinisa Todorovic

CS 261: Data Structures. Dynamic Array Queue

BBM 201 DATA STRUCTURES

Dynamic Storage Exercise

Big- (O): c.) binary search O(logn)

Intermediate Programming, Spring 2017*

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

Exercise 13 Dynamically Allocated Memory, Expression Trees. Informatik I für Mathematiker und Physiker (HS 2015) Yeara Kozlov

CS32 - Week 2. Umut Oztok. July 1, Umut Oztok CS32 - Week 2

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

CS 261: Data Structures. Sorted Dynamic Array Bag and Set

Kurt Schmidt. October 30, 2018

Quiz 0 Review Session. October 13th, 2014

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

CS261 Data Structures. Iterator ADT Dynamic Array and Linked List

CS201 Some Important Definitions

CSCI 262 Data Structures. The Big 3. Copy Constructor. Destructor. Assignment Operator 3/4/ The Big 3

Function Terminology

Dynamic Memory Allocation

Structured Data. CIS 15 : Spring 2007

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

CS113: Lecture 4. Topics: Functions. Function Activation Records

An Introduction to C++

CS 610: Intermediate Programming: C/C++ Making Programs General An Introduction to Linked Lists

CS510 Operating System Foundations. Jonathan Walpole

Elementary Data Structures: Lists

APS105. Malloc and 2D Arrays. Textbook Chapters 6.4, Datatype Size

Spring 2007 May 16, 2007 CS107 Midterm Solution

Programming in C++: Assignment Week 1

DS Assignment I. 1. Set a pointer by name first and last to point to the first element and last element of the list respectively.

ENCM 339 Fall 2017 Tutorial for Week 8

CS261 Data Structures. Maps (or Dic4onaries)

Data Representation and Storage

CSCI-UA /2. Computer Systems Organization Lecture 19: Dynamic Memory Allocation: Basics

Algorithms, Data Structures, and Problem Solving

CS 103 Unit 11. Linked Lists. Mark Redekopp

Tutorial 5. Overflow. Data Types. Structures. Call stack. Stack frames. Debugging Tips. CS 136 Winter 2019 Tutorial 5 1

Dynamic Memory: Alignment and Fragmentation

Data Structures in C. C Programming and Software Tools. N.C. State Department of Computer Science

C Structures & Dynamic Memory Management

SYSC 2006 C Winter 2012

CS 61C: Great Ideas in Computer Architecture C Memory Management, Usage Models

4. The Abstraction: The Process

CS 261. List Bag List Queue List Deque. by Tim Budd Ron Metoyer Sinisa Todorovic

Pointers, Dynamic Data, and Reference Types

Review! Lecture 5 C Memory Management !

CS61C : Machine Structures

CS 103 Unit 11. Linked Lists. Mark Redekopp

Fall 2018 Discussion 2: September 3, 2018

Material Sept 6. CS1706 Intro to Object Oriented Dev II - Spring 04

CS 137 Part 5. Pointers, Arrays, Malloc, Variable Sized Arrays, Vectors. October 25th, 2017

Introduction to Programming in C Department of Computer Science and Engineering

Array Based Lists. Collections

CSCI 104. Rafael Ferreira da Silva. Slides adapted from: Mark Redekopp and David Kempe

COMP 524 Spring 2018 Midterm Thursday, March 1

What is Class? Remember

Dynamic Data Structures. CSCI 112: Programming in C

Introduction to Computer Science Midterm 3 Fall, Points

What is an algorithm?

CS 241 Honors Memory

Unit 14. Passing Arrays & C++ Strings

CS 261 Data Structures. Priority Queue ADT & Heaps

Advanced Pointer & Data Storage

Symbol Tables. ASU Textbook Chapter 7.6, 6.5 and 6.3. Tsan-sheng Hsu.

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

IV Unit Second Part STRUCTURES

Memory Management. CS31 Pascal Van Hentenryck CS031. Lecture 19 1

CS261: HOMEWORK 2 Due 04/13/2012, at 2pm

CS32 Discussion Sec.on 1B Week 2. TA: Zhou Ren

Data Structures and Algorithms (DSA) Course 5 Lists. Iulian Năstac

[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 9. Seungkyu Lee. Assistant Professor, Dept. of Computer Engineering Kyung Hee University

Lecture 8 Dynamic Memory Allocation

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

14. Memory API. Operating System: Three Easy Pieces

Arrays and Pointers in C & C++

Linked Lists in C and C++

Evolving Frama-C Value Analysis

Downloaded S. from Kiran, PGT (CS) KV, Malleswaram STRUCTURES. Downloaded from

Apply to be a Meiklejohn! tinyurl.com/meikapply

COL106: Data Structures and Algorithms. Ragesh Jaiswal, IIT Delhi

Memory and C/C++ modules

AGENDA Binary Operations CS 3330 Samira Khan

ECE 2400 Computer Systems Programming Fall 2017 Topic 7: C Lists and Vectors

Week 9 Part 1. Kyle Dewey. Tuesday, August 28, 12

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

CMPS 105 Systems Programming. Prof. Darrell Long E2.371

Short Notes of CS201

C0MP1921/2011/2091 SAMPLE Final Exam Data Structures and Algorithms/Data Organisation/Computing 2

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

Basic Data Types. CS429: Computer Organization and Architecture. Array Allocation. Array Access

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Dynamic Memory Allocation I

CSCI 2132 Software Development. Lecture 29: Dynamic Memory Allocation

Final assignment: Hash map

CS201 - Introduction to Programming Glossary By

Linked data structures. EECS 211 Winter 2019

CSE 333 Midterm Exam Sample Solution 7/28/14

Memory management. Johan Montelius KTH

Transcription:

CS 261: Data Structures Dynamic Arrays Introduction

Arrays Core data structure Example: double arraybag[100]; struct Students{ int count; }; char initials[2]; 2

Positives: Arrays -- Pros and Cons Simple Each element accessible in O(1) Negatives: Size must be fixed when created What happens when the program later requires more space? 3

Dynamic Arrays Our goal: Hide memory management details behind an Application Program Interface (API) Each element is still accessible in O(1) But a dynamic array can change capacity 4

Size and Capacity data Size Capacity Unused elements 5

Size: Size and Capacity Current number of elements Managed by an internal data value Capacity: Number of elements that a dynamic array can hold before it must resize 6

Dynamic Array data = size = cap = 10 16 Size ( = size) Capacity ( = cap) 7

Adding an Element Increment the size Put the new value at the end of the dynamic array 8

Adding an Element What happens when size == capacity? Must: reallocate new space copy all data values to the new space hide these details from the user 9

Reallocate and Copy Before reallocation: After reallocation: data = data = size = 8 size = 8 cap = 8 cap = 16 Must allocate new (larger) array and copy valid data elements 10

Reallocate and Copy Before reallocation: After reallocation: data = data = size = cap = 8 8 old size = cap = 8 16 new DO NOT forget to free up the memory of the old array 11

Inserting an Element in the Middle May also require reallocation When? Requires that some elements be moved up to make space for the new one 12

Inserting an Element Loop from THE END backward while copying Add at idx à idx à Before After 13

Inserting an Element -- Complexity O(n) in the worst case 14

Removing an Element Remove also requires looping. Loop from idx forward while copying Remove idx à Before After 15

Removing an Element -- Complexity O(n) worst case 16

Interface View of Dynamic Arrays 17

General Purpose Dynamic Array Define TYPE as a symbolic preprocessor constant. Default double. Requires recompiling the source code if new types are needed 18

Interface file: dynarr.h struct dyarr { TYPE * data; /* Pointer to data array */ int size; /* Number of elements */ int capacity; /* Capacity of array */ }; /* Rest of dynarr.h on next slide */ 19

Interface (continued) /* function prototypes */ void initdynarr (struct dyarr *da, int cap); void freedynarr (struct dyarr *da); void adddynarr (struct dyarr *da, TYPE val); TYPE getdynarr (struct dyarr *da, int idx); void putdynarr (struct dyarr *da, int idx, TYPE val); int sizedynarr (struct dyarr *da); void _dyarrdoublecapacity (struct dyarray * da); 20

Implementation View of Dynamic Arrays 21

initdynarr -- Initialization /* Allocate memory to data array */ void initdynarr (struct dyarr *da, int cap){ assert (cap >= 0); da->capacity = cap; da->size = 0; da->data = (TYPE *) malloc(da->capacity * sizeof(type)); assert (da->data!= 0); /* check the status */ } 22

freedynarr -- Clean-up void freedynarr (struct dyarr * da) { assert (da!= 0); free (da->data); /*free entire array*/ da->capacity = 0; da->size = 0; } 23

Size int sizedynarr (struct dyarr * da){ } return da->size; 24

Get the Value at a Given Position TYPE getdynarr (struct dyarr *da, int idx); { /*always make sure the input is meaningful*/ assert((sizedynarr(da) > idx) && (idx >= 0)); return da->data[idx]; } why? 25

Add a New Element void adddynarr (struct dyarr * da, TYPE val){ /*make sure there is enough capacity*/ if (da->size >= da->capacity) _dyarrdoublecapacity(da); da->data[da->size] = val; da->size++; /*must increase the size*/ } 26

Double the Capacity Before reallocation: After reallocation: data = data = size = cap = 8 8 old size = cap = 8 16 new MUST: 1. allocate new space 2. copy data to new space 3. free old space 27

Double the Capacity void _dyarrdoublecapacity (struct dyarray * da) { TYPE * oldbuffer = da->data; /*memorize old*/ int oldsize = da->size; /*allocate new memory*/ initdynarr (da, 2 * da->capacity); for (int i = 0; i < oldsize; i++) /*copy old*/ da->data[i] = oldbuffer[i]; da->size = oldsize; free(oldbuffer); /*free old memory*/ } 28

Next Class How to implement Stack Bag by using Dynamic Array 29