Dynamic Storage Exercise. We saw that by int i; while (std::cin >> i)... we can read inputs as long as there are more available in std::cin. Your task.

Slides:



Advertisements
Similar presentations
Chapter 19 Vectors, templates, and exceptions Bjarne Stroustrup
Advertisements

Chapter 18 Vectors and Arrays
Lecture 6 Sept 11, 2008 Goals for the day: Linked list and project # 1 list class in STL (section 3.3) stack – implementation and applications.
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Chapter 18 Vectors and Arrays John Keyser’s Modification of Slides by Bjarne Stroustrup
Chapter 17 vector and Free Store John Keyser’s Modifications of Slides By Bjarne Stroustrup
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
CS252: Systems Programming Ninghui Li Program Interview Questions.
Lecture 6 Hashing. Motivating Example Want to store a list whose elements are integers between 1 and 5 Will define an array of size 5, and if the list.
Basic Algorithms on Arrays. Learning Objectives Arrays are useful for storing data in a linear structure We learn how to process data stored in an array.
Data Structures: A Pseudocode Approach with C
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
What’s it all about STL vectors offer amortized constant time insertion, by doubling the capacity whenever it’s full Vectors also offer contiguous memory,
Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas.
Dynamic Objects. COMP104 Lecture 31 / Slide 2 Static verses Dynamic Objects * Static object n Memory is acquired automatically  int A[10]; n Memory is.
Next Section: Pointer Analysis Outline: –What is pointer analysis –Intraprocedural pointer analysis –Interprocedural pointer analysis (Wilson & Lam) –Unification.
Dynamic Objects. COMP104 Dynamic Objects / Slide 2 Memory Management * Static Memory Allocation n Memory is allocated at compilation time * Dynamic Memory.
Pointers and dynamic objects COMP171 Fall Pointers and dynamic objects/ Slide 2 Topics * Pointers n Memory addresses n Declaration n Dereferencing.
Tirgul 8 Universal Hashing Remarks on Programming Exercise 1 Solution to question 2 in theoretical homework 2.
Value Semantics CS-240 & CS-341 Dick Steflik. Value Semantics determine how the value(s) of one object are copied to another object in C++ the value semantics.
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
Wednesday, 11/6/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/6/02  QUESTIONS?? – HW # 4 due Monday  Today:  Return HW #3  Arrays (Chap. 10)  Reading:
Lecture 6 Hashing. Motivating Example Want to store a list whose elements are integers between 1 and 5 Will define an array of size 5, and if the list.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Program Structure. r Memory management is designed to be transparent to the user program r As a programmer you typically do not think about how memory.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
CS261 Data Structures Dynamic Arrays Part 2: Implementation.
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Dynamic Memory Allocation 9.8.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Pointers review Let a variable aa be defined as ‘int *aa;’, what is stored in aa? Let a variable aa be defined as ‘int ** aa;’ what is stored in aa? Why.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
ECE556 Project Part 1 Azadeh Davoodi Spring 2015.
1. The term STL stands for ? a) Simple Template Library b) Static Template Library c) Single Type Based Library d) Standard Template Library Answer : d.
Memory Management Issues, Solutions, and Examples.
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
Dynamic Memory. We will follow different order from Course Book We will follow different order from Course Book First we will cover Sect The new.
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
C arrays are limited: -they are represented by pointers (which may or may not be valid); -Indexes not checked (which means you can overrun your array);
Advanced Algorithm Design and Analysis (Lecture 12) SW5 fall 2004 Simonas Šaltenis E1-215b
Homework due Test the random number generator Create a 1D array of n ints Fill the array with random numbers between 0 and 100 Compute and report the average.
1 Chapter 1 C++ Templates Sections 1.6 and Templates Type-independent patterns that can work with multiple data types –Generic programming –Code.
Assembly - Arrays תרגול 7 מערכים.
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
Revision on C++ Pointers TCP1201: 2013/2014. Pointer Basics  Why pointer is important? 1. Reference/Point to existing data without cloning the data.
Object Oriented Programming COP3330 / CGS5409.  Aggregation / Composition  Dynamic Memory Allocation.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Lecture 7.  There are 2 types of libraries used by standard C++ The C standard library (math.h) and C++ The C++ standard template library  Allows us.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
1 CMPT 117 Linked Lists (singly linked). 2 Problems with arrays  When an element is deleted from or inserted into an array, the rest of the array has.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
Object Oriented Programming COP3330 / CGS5409
Linked List Intro CSCE 121 J. Michael Moore.
Dynamic Memory A whole heap of fun….
The Standard Template Library
Dynamic Memory A whole heap of fun….
Linked List Intro CSCE 121.
Lecture-Hashing.
Presentation transcript:

Dynamic Storage Exercise

We saw that by int i; while (std::cin >> i)... we can read inputs as long as there are more available in std::cin. Your task is to write a code snippet which reads inputs as described above, and which then stores these inputs in an array. For this exercise you are not allowed to use the Standard Library (i.e. no std::vector ). To achieve this you will have to use new[] and delete[]. 2 (From: Script Exercise 158.a)

Dynamic Storage Solution Idea: 1.Allocate some range (using new[] ) 2.As soon as range full, allocate larger range (using new[] ) 3.Copy over initial range 4.Delete initial range (using delete[] ) 5.Go back to 2. with newly generated memory 3 (From: Script Exercise 158.a)

Dynamic Storage Solution Idea: 1.Allocate some range (using new[] ) 2.As soon as range full, allocate larger range (using new[] ) 3.Copy over initial range 4.Delete initial range (using delete[] ) 5.Go back to 2. with newly generated memory (From: Script Exercise 158.a)

Dynamic Storage Solution Idea: 1.Allocate some range (using new[] ) 2.As soon as range full, allocate larger range (using new[] ) 3.Copy over initial range 4.Delete initial range (using delete[] ) 5.Go back to 2. with newly generated memory (From: Script Exercise 158.a)

Dynamic Storage Solution Idea: 1.Allocate some range (using new[] ) 2.As soon as range full, allocate larger range (using new[] ) 3.Copy over initial range 4.Delete initial range (using delete[] ) 5.Go back to 2. with newly generated memory (From: Script Exercise 158.a)

Dynamic Storage Solution Idea: 1.Allocate some range (using new[] ) 2.As soon as range full, allocate larger range (using new[] ) 3.Copy over initial range 4.Delete initial range (using delete[] ) 5.Go back to 2. with newly generated memory (From: Script Exercise 158.a)

Dynamic Storage Solution Idea: 1.Allocate some range (using new[] ) 2.As soon as range full, allocate larger range (using new[] ) 3.Copy over initial range 4.Delete initial range (using delete[] ) 5.Go back to 2. with newly generated memory (From: Script Exercise 158.a)

Dynamic Storage Solution Idea: 1.Allocate some range (using new[] ) 2.As soon as range full, allocate larger range (using new[] ) 3.Copy over initial range 4.Delete initial range (using delete[] ) 5.Go back to 2. with newly generated memory (From: Script Exercise 158.a)

Dynamic Storage Solution Idea: 1.Allocate some range (using new[] ) 2.As soon as range full, allocate larger range (using new[] ) 3.Copy over initial range 4.Delete initial range (using delete[] ) 5.Go back to 2. with newly generated memory (From: Script Exercise 158.a)

Dynamic Storage Solution Idea: 1.Allocate some range (using new[] ) 2.As soon as range full, allocate larger range (using new[] ) 3.Copy over initial range 4.Delete initial range (using delete[] ) 5.Go back to 2. with newly generated memory (From: Script Exercise 158.a)

Dynamic Storage Solution New range… How much larger? much larger  Pro: ranges less often full  copy ranges less often Con:larger memory consumption Important: Larger by a factor, not by a constant… length_n = length_o * 2 length_n = length_o (From: Script Exercise 158.a)

Dynamic Storage Solution Larger by: a) factor 2b) constant 2 13 elementsCase a)Case b) (From: Script Exercise 158.a)

Dynamic Storage Solution Larger by: a) factor 2b) constant 2 14 elementsCase a)Case b) arbitr. chosen (From: Script Exercise 158.a)

Dynamic Storage Solution Larger by: a) factor 2b) constant 2 15 elementsCase a)Case b) Case a): Significantly fewer resizings. Case a): Significantly fewer resizings. arbitr. chosen (From: Script Exercise 158.a)

Dynamic Storage Solution Larger by: a) factor 2b) constant 2 16 elementsCase a)Case b) arbitr. chosen Each resizing means: Copy WHOLE array. Each resizing means: Copy WHOLE array. Case a): Significantly fewer resizings. Case a): Significantly fewer resizings. (From: Script Exercise 158.a)

Dynamic Storage Solution Larger by: a) factor 2b) constant 2 17 elementsCase a)Case b) arbitr. chosen Each resizing means: Copy WHOLE array. Each resizing means: Copy WHOLE array. Case a): Significantly fewer resizings. Case a): Significantly fewer resizings. Factor 2 is an arbitrary, but good choice. (From: Script Exercise 158.a)

Dynamic Storage Solution And the code… 18 int n = 1; // current array size int k = 0; // number of elements read so far // dynamically allocate array int* a = new int[n]; // this time, a is NOT a constant // read into the array while (std::cin >> a[k]) { if (++k == n) { // next element wouldn't fit; replace the array a by // a new one of twice the size int* b = new int[n*=2]; // get pointer to new array for (int i=0; i<k; ++i) // copy old array to new one b[i] = a[i]; delete[] a; // delete old array a = b; // let a point to new array } (From: Script Exercise 158.a)

By the way, … … this is exactly how my_vec.push_back(...) works. push_back is a member function. … all dynamic containers (vector, set, list, …) are based on new, delete ! 19

Vector …

Dynamic Storage in Vectors Vectors store 3 pointers: begin :begin of memory end :end of user-accessible part end2 :end of allocated part

Dynamic Storage in Vectors Important for vectors: In constructor:Set initial range In copy-constructor:Don’t copy just pointers; i.e. copy the ranges behind them In operator= :Like copy-constructor, in addition: i)prevent self-assignments ii)don’t forget to delete old range 22