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

Similar documents
CS 261: Data Structures. Dynamic Arrays. Introduction

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

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

CS 261: Data Structures. Dynamic Array Queue

CS 261: Data Structures. Dynamic Array Queue

Kurt Schmidt. October 30, 2018

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

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

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

BBM 201 DATA STRUCTURES

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

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

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

struct Properties C struct Types

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 2. Spring 2016

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

Array Based Lists. Collections

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

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

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

CS261 Data Structures. Iterator ADT Dynamic Array and Linked List

CSE 333 Midterm Exam Sample Solution 7/28/14

CS201 Some Important Definitions

Quiz 0 Review Session. October 13th, 2014

Dynamic Data Structures. CSCI 112: Programming in C

CS 580 FINAL EXAM. Fall April 29, 2014

CS 2113 Software Engineering

CS240: Programming in C

Fall 2018 Discussion 2: September 3, 2018

Structured Data. CIS 15 : Spring 2007

CSCI 171 Chapter Outlines

CS Programming I: ArrayList

Advanced Pointer & Data Storage

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

Introducing C++ to Java Programmers

CSI33 Data Structures

Implementing Abstractions

Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso

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

An Introduction to C++

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

Orbix TS Thread Library Reference

Object-Oriented Programming

Data Representation and Storage

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

Apply to be a Meiklejohn! tinyurl.com/meikapply

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.

PESIT-BSC Department of Science & Humanities

ENCM 339 Fall 2017 Tutorial for Week 8

CS3215. Outline: 1. Introduction 2. C++ language features 3. C++ program organization

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

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

Stack & Queue on Self-Referencing Structures

Topic 6: A Quick Intro To C

200 points total. Start early! Update March 27: Problem 2 updated, Problem 8 is now a study problem.

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

SYSC 2006 C Winter 2012

Lecture 8 Dynamic Memory Allocation

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

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

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

EXAMINATIONS 2012 MID YEAR. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

III. Classes (Chap. 3)

Arrays and ArrayLists. Ananda Gunawardena

CS 103 Unit 11. Linked Lists. Mark Redekopp

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

Slide Set 6. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Dynamic Memory: Alignment and Fragmentation

C Structures & Dynamic Memory Management

Lectures 5-6: Introduction to C

How about them A s!! Go Oaktown!! CS61C - Machine Structures. Lecture 4 C Structures Memory Management Dan Garcia.

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

4. The Abstraction: The Process

COSC Software Engineering. Lecture 16: Managing Memory Managers

Pointers, Dynamic Data, and Reference Types

Robust Programming. Style of programming that prevents abnormal termination and unexpected actions

CP222 Computer Science II. Linked Lists

Chapter 19: Program Design. Chapter 19. Program Design. Copyright 2008 W. W. Norton & Company. All rights reserved.

Adam Blank Lecture 2 Winter 2019 CS 2. Introduction to Programming Methods

Dynamic Allocation in C

Dynamic Storage Exercise

Come and join us at WebLyceum

CS 103 Unit 11. Linked Lists. Mark Redekopp

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

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

Function Terminology

Comp 11 - Summer Session Hashmap


Introduction to Computer Science Midterm 3 Fall, Points

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

CSE 333 Midterm Exam 5/9/14 Sample Solution

CS S-08 Arrays and Midterm Review 1

Motor Industry Software Reliability Association (MISRA) C:2012 Standard Mapping of MISRA C:2012 items to Goanna checks

CSE 374 Final Exam Sample Solution 3/15/12

CS113: Lecture 9. Topics: Dynamic Allocation. Dynamic Data Structures

Elementary Data Structures: Lists

Transcription:

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

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

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

Dynamic Arrays in Java called: Vector, ArrayList Our goal: Hide memory management details behind a simple API Each element is still accessible in O(1) But a dynamic array can change capacity

Size and Capacity data Size Capacity Unused elements

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

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

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

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

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

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

Adding an Element to Middle May also require reallocation When? Will ALWAYS require elements to be moved up to make space

Adding an Element to Middle Loop from the end while copying data! Add at idx à idx à Before After

Adding an Element to Middle Complexity? O(n) in the worst case

Removing an Element Requires sliding over to delete the value

Removing an Element Remove also requires loop. This time should it be from the end? Remove idx à Before After

Removing an Element Complexity? O(n) worst case

Interface View of Dynamic Arrays

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

Interface file: dynarr.h #ifndef _DyArray_H #define _DyArray_H # define TYPE double # define TYPE_SIZE sizeof(type) # define LT(a, b) (a < b) # define EQ(a, b) (a == b)... /* Rest of dynarr.h on next slide */ # endif

Interface (continued) 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 */

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);

Implementation View of Dynamic Arrays

initdynarr -- Initialization 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); }

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

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

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

Add a New Element void adddynarr (struct dyarr * da, TYPE val){ if (da->size >= da->capacity) _dyarrdoublecapacity(da); da->data[da->size] = val; da->size++; }

Double the Capacity Before reallocation: After reallocation: data = data = size = 8 size = 8 cap = 8 cap = 16 Must: allocate space copy data free old space

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

Next Class How to implement Stack Bag by using Dynamic Array