Implementing vector<>

Similar documents
vector and Free Store

Chapter 17 vector and Free Store

Chapter 17 vector and Free Store

Chapter 17 vector and Free Store. Bjarne Stroustrup

Abstract. Vector. Overview. Building from the ground up. Building from the ground up 1/8/10. Chapter 17 vector and Free Store

Chapter 18 Vectors and Arrays [and more on pointers (nmm) ] Bjarne Stroustrup

The vector Class and Memory Management Chapter 17. Bjarne Stroustrup Lawrence "Pete" Petersen Walter Daugherity Fall 2007

Vector and Free Store (Pointers and Memory Allocation)

Vector and Free Store (Vectors and Arrays)

Chapter 19 Vector, templates, and exceptions

Pointers and Memory 1

C++ Programming. Classes, Constructors, Operator overloading (Continued) M1 Math Michail Lampis

Overview. Chapter 18 Vectors and Arrays. Reminder. vector. Bjarne Stroustrup

Copy Constructors & Destructors

Standard-Library Exception Safety

We look back... #include <iostream> #include <vector>

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

int main(){ int main(){ We want to understand this in depth! std::vector<int> v(10,0); // Vector of length 10

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. C++ Kitchen Sink. Lecture 14.

Vector. Vector Class. class Vector { public: typedef unsigned int size_type;

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

CS11 Advanced C++ Spring 2018 Lecture 2

CS11 Advanced C++ Spring 2018 Lecture 1

CPSC 427: Object-Oriented Programming

Assignment of Objects

18. Dynamic Data Structures I. Dynamic Memory, Addresses and Pointers, Const-Pointer Arrays, Array-based Vectors

Managing Memory. (and low level Data Structures) Lectures 22, 23. Hartmut Kaiser.

CS24 Week 3 Lecture 1

Templates and Vectors

CSE 303: Concepts and Tools for Software Development

Section Handout #4: Classes, Pointers, and Dynamic Memory

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

Object-Oriented Principles and Practice / C++


9. C++ advanced (II): Templates

RAII and Smart Pointers. Ali Malik

CSC1322 Object-Oriented Programming Concepts

Classes and Objects. Class scope: - private members are only accessible by the class methods.

9. C++ advanced (II): Templates

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

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

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

C++ Programming. Pointers and Memory Management. M1 Math Michail Lampis

Assignment of Structs

CPSC 427: Object-Oriented Programming

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management

Concepts (this lecture) CSC 143. Classes with Dynamically Allocated Data. List of ints w/dups (cont.) Example: List of ints with dups.

Class Vector: Interface CSE 143. Dynamic Memory In Classes [Chapter 4, p ] Vector Implementation. Many Ways to Implement. Draw the picture!

Objects Managing a Resource

Classes and Pointers: Some Peculiarities

And Even More and More C++ Fundamentals of Computer Science

Link-Based Implementations. Chapter 4

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Written by John Bell for CS 342, Spring 2018

Announcements. Lecture 05a Header Classes. Midterm Format. Midterm Questions. More Midterm Stuff 9/19/17. Memory Management Strategy #0 (Review)

Programming Abstractions

Engineering Tools III: OOP in C++

Working with Batches of Data

A brief introduction to C++

Outline. Dynamic Memory Classes Dynamic Memory Errors In-class Work. 1 Chapter 10: C++ Dynamic Memory

Polymorphism. Programming in C++ A problem of reuse. Swapping arguments. Session 4 - Genericity, Containers. Code that works for many types.

COMP6771 Advanced C++ Programming

Flashback Technicalities: Classes, etc. Lecture 11 Hartmut Kaiser

Errors. Lecture 6. Hartmut Kaiser hkaiser/fall_2011/csc1254.html

CS93SI Handout 04 Spring 2006 Apr Review Answers

Exceptions and Design

PIC 10B Lecture 1 Winter 2014 Homework Assignment #3

Midterm Review. PIC 10B Spring 2018

04-19 Discussion Notes

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

Pointers. A pointer value is the address of the first byte of the pointed object in the memory. A pointer does not know how many bytes it points to.

C++ Programming Lecture 4 Software Engineering Group

Arrays - Vectors. Arrays: ordered sequence of values of the same type. Structures: named components of various types

Lecture 8. Exceptions, Constructor, Templates TDDD86: DALP. Content. Contents Exceptions

Consider the program...

Critique this Code. Hint: It will crash badly! (Next slide please ) 2011 Fawzi Emad, Computer Science Department, UMCP

Launchpad Lecture -10

CMSC 330: Organization of Programming Languages

CSE 333 Midterm Exam Sample Solution 5/10/13

Algorithms for Arrays Vectors Pointers CS 16: Solving Problems with Computers I Lecture #14

Object-Oriented Principles and Practice / C++

CSE 143. Linked Lists. Linked Lists. Manipulating Nodes (1) Creating Nodes. Manipulating Nodes (3) Manipulating Nodes (2) CSE 143 1

Ali Malik Handout 12 CS106L Winter 2018 Jan 30th, C++ Reference

THE GOOD, BAD AND UGLY ABOUT POINTERS. Problem Solving with Computers-I

A class is a user-defined type. It is composed of built-in types, other user-defined types and

Special Member Functions

Allocation & Efficiency Generic Containers Notes on Assignment 5

Chapter 9 Technicalities: Classes, etc.

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor

CSI33 Data Structures

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

PRACTICE MIDTERM EXAM #2

Outline. A C++ Linked Structure Class A C++ Linked List C++ Linked Dynamic Memory Errors In-class work. 1 Chapter 11: C++ Linked Structures

When we program, we have to deal with errors. Our most basic aim is correctness, but we must

Chapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

04-17 Discussion Notes

use static size for this buffer

STL Quick Reference for CS 241

Common Misunderstandings from Exam 1 Material

Transcription:

Lecture 26 Hartmut Kaiser hkaiser@cct.lsu.edu http://www.cct.lsu.edu/ hkaiser/fall_2011/csc1254.html

2 Vector Vector is the most useful container Simple Compactly stores elements of a given type Efficient access Expands to hold any number of elements Optionally range-checked access How is that done? That is, how is vector implemented? We'll answer that gradually, feature after feature Vector is the default container prefer vector for storing elements unless there's a good reason not to

3 Building from the ground up The hardware provides memory and addresses Low level Untyped Fixed-sized No checking As fast as the hardware architects can make it The application builder needs something like a vector Higher-level operations Type checked Size varies (as we get more data) Run-time checking Close-to optimally fast

4 Building from the ground up At the lowest level, close to the hardware, life s simple and brutal You have to program everything yourself You have no type checking to help you Run-time errors are found when data is corrupted or the program crashes We want to get to a higher level as quickly as we can To become productive and reliable To use a language fit for humans We showed all the steps needed The alternative to understanding is to believe in magic The techniques for building vector are the ones underlying all higher-level work with data structures

5 Vector A vector Can hold an arbitrary number of elements Up to whatever physical memory and the operating system can handle That number can vary over time E.g. by using push_back() Example: age: age[0]: age[1]: age[2]: age[3]: 4 0.33 22.0 27.2 54.2 vector<double> age(4); age[0] =.33; age[1] = 22.0; age[2] = 27.2; age[3] = 54.2;

6 Vector // a very simplified vector of doubles (like // vector<double>): class vector { int sz; // the number of elements ( the size ) double* elem; // pointer to the first element public: vector(int s); // constructor: allocate s elements, // let elem point to them // store s in sz int size() const { return sz; // the current size ; * means pointer to so double* is a pointer to double What is a pointer? How do we make a pointer point to elements? How do we allocate elements?

7 Vector (constructor) vector::vector(int s) // vector's constructor : sz(s), // store the size s in sz elem(new double[s]) // allocate s doubles on the free store // store a pointer to those doubles in elem { // Note: new does not initialize elements (but the standard // vector does) sz: elem: 4 A pointer Free store: new allocates memory from the free store and returns a pointer to the allocated memory

8 Pointers, arrays, and vector Note With pointers and arrays we are "touching" hardware directly with only the most minimal help from the language. Here is where serious programming errors can most easily be made, resulting in malfunctioning programs and obscure bugs Be careful and operate at this level only when you really need to vector is one way of getting almost all of the flexibility and performance of arrays with greater support from the language (read: fewer bugs and less debug time).

9 Vector (Construction and Primitive Access) // a very simplified vector of doubles: class vector { int sz; // the size double* elem; // a pointer to the elements public: vector(int s) : sz(s), elem(new double[s]) { // constructor double get(int n) { return elem[n]; // access: read void set(int n, double v) { elem[n] = v; // access: write int size() const { return sz; // the current size ; vector v(10); for (int i = 0; i < v.size(); ++i) { v.set(i, i); cout << v.get(i) << ' '; 10 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0

10 A Problem: Memory Leak void f(int x) { vector v(x); // define a vector // (which allocates x doubles on // the free store) //... use v... // give the memory allocated by v back to the free store // but how? (vector's elem data member is private)

11 Vector (Destructor) // a very simplified vector of doubles: class vector { int sz; // the size double* elem; // a pointer to the elements public: vector(int s) // constructor: allocates/acquires memory : sz(s), elem(new double[s]) { ~vector() { // destructor: de-allocates/releases memory delete[] elem; // ; Note: this is an example of a general and important technique: Acquire resources in a constructor Release them in the destructor Examples of resources: memory, files, locks, threads, sockets

12 A Problem: Memory Leak void f(int x) { int* p = new int[x]; // allocate x ints vector v(x); // define a vector (which allocates // another x ints) //... use p and v... delete[] p; // deallocate the array pointed to by p // the memory allocated by v is implicitly deleted here by // vector's destructor The delete now looks verbose and ugly How do we avoid forgetting to delete[] p? Experience shows that we often forget Prefer deletes in destructors

13 A Problem Copy doesn t work as we would have hoped (expected?) void f(int n) { vector v(n); vector v2 = v; vector v3; v3 = v; // // define a vector // what happens here? // what would we like to happen? // what happens here? // what would we like to happen? Ideally: v2 and v3 become copies of v (that is, = makes copies) And all memory is returned to the free store upon exit from f() That s what the standard vector does, but it s not what happens for our still-too-simple vector

14 Naïve Copy Initialization (the default) v1: 3 void f(int n) { vector v1(n); v2: 3 vector v2 = v1; // initialization: // by default, a copy of a class copies its members // so sz and elem are copied Disaster when we leave f()! v1 s elements are deleted twice (by the destructor)

15 Naïve Copy Assignment (the default) v1: 3 void f(int n) v2: 3 { 1 st vector v1(n); vector v2(4); v2 = v1; // assignment: // by default, a copy of a class copies its members // so sz and elem are copied Disaster when we leave f()! v1 s elements are deleted twice (by the destructor) memory leak: v2 s elements are not deleted 2 nd

16 Copy Constructor (Initialization) class vector { int sz; double* elem; public: vector(vector const&); // copy constructor: define copy //... ; vector::vector(vector const& a) : sz(a.sz), elem(new double[a.sz]) // allocate space for elements, then // initialize them (by copying) { for (int i = 0; i < sz; ++i) elem[i] = a.elem[i];

17 Copy with Copy Constructor v1: 3 v2: void f(int n) { vector v1(n); vector v2 = v1; // copy using the copy constructor // a for loop copies each value from v1 into v2 3 The destructor correctly deletes all elements (once only)

18 Copy Assignment x: class vector { int sz; 2 nd double* elem; public: vector& operator=(vector const & a); // copy assignment: define copy //... ; a: 1 st 1 2 3 4 8 4 2 8 4 2 Memory leak? x = a; Operator = must copy a s elements

19 Copy Assignment vector& vector::operator=(vector const& a) // like copy constructor, but we must deal with old elements // make a copy of a then replace the current sz and elem with a s { double* p = new double[a.sz]; // allocate new space for (int i = 0; i<a.sz; ++i) p[i] = a.elem[i]; // copy elements delete[ ] elem; sz = a.sz; elem = p; // deallocate old space // set new size // set new elements return *this; // return a self-reference // The this pointer is explained in chapter 14

20 Copy with copy assignment v1: 3 6 24 42 delete[ ]d by = v2: 3 1 st void f(int n) { vector v1(n); vector v2(4); v2 = v1; 2 nd 6 // assignment 24 42 No memory Leak!

21 Copy Terminology Shallow copy: copy only a pointer so that the two pointers now refer to the same object What pointers and references do Deep copy: copy the pointer and also what it points to so that the two pointers now each refer to a distinct object What vector, string, etc. do Requires copy constructors and copy assignments for container classes x: Copy of x: x: Copy of x: Shallow copy y: y: Copy of y: Deep copy

22 Deep and Shallow Copy v1: 2 2 4 vector<int> v1; v1.push_back(2); v1.push_back(4); vector<int> v2 = v1; // elements) v2[0] = 3; // v1[0] is still 2 v2: 2 2 3 4 // deep copy (v2 gets its own copy of v1 s r2: r1: b: 9 7 int b = 9; int& r1 = b; int& r2 = r1; // shallow copy (r2 refers to the same variable as r1) r2 = 7; // b becomes 7

23 Vector (primitive access) // a very simplified vector of doubles: vector v(10); for (int i = 0; i < v.size(); ++i) { v.set(i, i); cout << v.get(i); for (int i = 0; i < v.size(); ++i) { v[i] = i; cout << v[i]; // pretty ugly: // we re used to this: 10 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0

24 Vector (We could use Pointers for Access) // a very simplified vector of doubles: class vector { int sz; // the size double* elem; // pointer to elements public: vector(int s) : sz(s), elem(new double[s]) { double* operator[](int n) { return &elem[n]; ; // constructor // return pointer vector v(10); for (int i = 0; i < v.size(); ++i) { // works, but still too ugly: *v[i] = i; // means *(v[i]), that is return a pointer // to the ith element, and dereference it cout << *v[i]; 10 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0

25 Vector (We use References for Access) // a very simplified vector of doubles: class vector { int sz; // the size double* elem; // pointer to elements public: vector(int s) : sz(s), elem(new double[s]) { // constructor double& operator[](int n) { return elem[n]; // return // reference ; vector v(10); for (int i = 0; i < v.size(); ++i) { // works and looks right! v[i] = i; // v[i] returns a reference to the ith element cout << v[i]; 10 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0