Tim Ehrlich Growing Arrays in C.

Slides:



Advertisements
Similar presentations
Linked Lists.
Advertisements

CS4432: Database Systems II Hash Indexing 1. Hash-Based Indexes Adaptation of main memory hash tables Support equality searches No range searches 2.
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
CS261 Data Structures Dynamic Arrays. Pro: only core data structure designed to hold a collection of elements Pro: random access: can quickly get to any.
CPSC 388 – Compiler Design and Construction
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Hashing CS 3358 Data Structures.
FALL 2004CENG 3511 Hashing Reference: Chapters: 11,12.
METU Department of Computer Eng Ceng 302 Introduction to DBMS Disk Storage, Basic File Structures, and Hashing by Pinar Senkul resources: mostly froom.
Hashing General idea: Get a large array
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
CHP - 9 File Structures. INTRODUCTION In some of the previous chapters, we have discussed representations of and operations on data structures. These.
Today Review of Directory of Slot Block Organizations Heap Files Program 1 Hints Ordered Files & Hash Files RAID.
File Processing - Indexing MVNC1 Indexing Jim Skon.
CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
File Structures. 2 Chapter - Objectives Disk Storage Devices Files of Records Operations on Files Unordered Files Ordered Files Hashed Files Dynamic and.
Hash Table March COP 3502, UCF 1. Outline Hash Table: – Motivation – Direct Access Table – Hash Table Solutions for Collision Problem: – Open.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Chapter 5 Record Storage and Primary File Organizations
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
Linked Lists Source: presentation based on notes written by R.Kay, A. Hill and C.Noble ● Lists in general ● Lists indexed using pointer arrays ● Singly.
Insertion sort Loop invariants Dynamic memory
Sections 10.5 – 10.6 Hashing.
Memory Hierarchy Ideal memory is fast, large, and inexpensive
ECE Application Programming
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
CHP - 9 File Structures.
Arrays (review) CSE 2011 Winter May 2018.
Introduction to Linked Lists
Linked Lists Chapter 6 Section 6.4 – 6.6
Review Deleting an Element from a Linked List Deletion involves:
CS522 Advanced database Systems
Priority Queues and Heaps
Hashing CSE 2011 Winter July 2018.
Dynamic Memory Allocation
Hashing CENG 351.
CNG 140 C Programming (Lecture set 10)
Lecture 10: Buffer Manager and File Organization
Introduction to Linked Lists
Stacks, Queues, and Deques
Disk Storage, Basic File Structures, and Hashing
Object Oriented Programming COP3330 / CGS5409
Stacks, Queues, and Deques
Linked List Intro CSCE 121 J. Michael Moore.
Resolving collisions: Open addressing
CS222: Principles of Data Management Notes #8 Static Hashing, Extendible Hashing, Linear Hashing Instructor: Chen Li.
Hash-Based Indexes Chapter 10
Dynamic Memory Allocation
Growing Arrays in C Russell Stephens.
CS222P: Principles of Data Management Notes #8 Static Hashing, Extendible Hashing, Linear Hashing Instructor: Chen Li.
Pointer Data Type and Pointer Variables III
CS Data Structure: Heaps.
A Robust Data Structure
Index tuning Hash Index.
CACHE-CONSCIOUS INDEXES
EE 312 Software Design and Implementation I
Pointer Variables A pointer is a variable that contains a memory address The address is commonly the location of another variable in memory This pointer.
Data Structures Unsorted Arrays
Data Structures & Algorithms
Stacks, Queues, and Deques
Linked List Intro CSCE 121.
EE 312 Software Design and Implementation I
Lecture-Hashing.
Presentation transcript:

Tim Ehrlich Growing Arrays in C

Other structures: Previously seen/used arrays have been static. Size and contents of the array are set when the program is compiled, and cannot be changed. Hash tables are more appropriate for larger, modifiable data structures.

Considerations: Adding elements to a sorted array takes n2 time. Large values of n will take exponentially longer to add, making it unattractive. For a small number of variables, a growing array is appropriate. Resizing the array in set chunks can reduce allocation cost in terms of time and space. Growing array elements must be called by subscripts, not pointers, since the array address can change upon resize.

Code, pg. 42 Growing the array by a factor of 2 (doubling it) provides a simple and fast way of changing its size. Growing an array will preserve the elements already in the array, and will also keep them if the grow fails. In this example, items are added to the end of the array, and the array is automatically grown if necessary. An error will be returned if the grow fails (out of memory or other error).

Code, pg. 43 You can delete an element from any position in the array, but the program has to handle filling that gap This program will move the elements down 1 by 1 to fill the gap, which will preserve the element’s order. C contains functions for moving memory Memcpy is faster, but overwrites if source and destination overlap Memmove is slower, but will always be correct (most often used)

Code, pg. 44 Memmove Memmove(s1, s2, size n) Will copy n bytes from location defined by s2 to location defined by s1 Operates as if s2 is first copied to temporary array, then from there to s1. a, b, c, _, e, f, g Memmove(4, 5, 3) a, b, c, e, f, g, _

Memmove Avoids copying elements in wrong order Deleting: start at beginning and move elements down Inserting: start at end and move elements up A for loop would require remembering to increment or decrement appropriately. Memmove you simply define the starting and ending location, and it will know to do the appropriate operation to avoid overwriting data.

Deletion alternatives Mark elements unused Set value to null Search for null value in array first and add new element there, otherwise add to end (does not preserve order) Leave null gaps in arrays (not a good practice, but occasionally has a use) Pull element off end of array and move single element into deleted slot. (does not preserve order)

Recap For small or fixed-size sets of data, arrays are more functional and more efficient than other structures. Arrays provide fast access to data, and better support quick sorting or binary search, etc. They also have minimal overhead requirements. Maintaining changing values is where arrays get “expensive”, so if the size of the array is unknown, or could be / become large, other structures are better.