Chapter 17 vector and Free Store Bjarne Stroustrup www.stroustrup.com/Programming.

Slides:



Advertisements
Similar presentations
UNIT 9: Pointers Data Variable and Pointer Variable Pass by Reference
Advertisements

Chapter 5 Errors Bjarne Stroustrup
Chapter 8 Technicalities: Functions, etc. Bjarne Stroustrup
Chapter 19 Vectors, templates, and exceptions Bjarne Stroustrup
Chapter 7 Completing a Program
Chapter 14 Graph class design Bjarne Stroustrup
Chapter 4 Computation Bjarne Stroustrup
Chapter 18 Vectors and Arrays
Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 11 Memory Management C makes it easy to shoot.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
1 Linked Lists III Template Chapter 3. 2 Objectives You will be able to: Write a generic list class as a C++ template. Use the template in a test program.
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Pointers and Arrays Chapter 12
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.
Data Structures Using C++ 2E
Chapter 18 Vectors and Arrays John Keyser’s Modification of Slides by Bjarne Stroustrup
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Chapter 17 vector and Free Store John Keyser’s Modifications of Slides By Bjarne Stroustrup
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Informática II Prof. Dr. Gustavo Patiño MJ
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.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Lecture 11 vector and Free Store Bjarne Stroustrup
Slides adapted from: Bjarne Stroustrup, Programming – Principles and Practice using C++ Chapter 17 vector and Free Store Hartmut Kaiser
Runtime Environments Compiler Construction Chapter 7.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
CSCE 121: Introduction to Program Design and Concepts, Honors Dr. J. Michael Moore Spring 2015 Set 16: Vector and Free Store CSCE : Set 16:
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
SPL – Practical Session 2 Topics: – C++ Memory Management – Pointers.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
Dynamically Allocated Arrays December 4, Skip the Rest of this PowerPoint.
Dynamic memory allocation and Pointers Lecture 4.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
Object-Oriented Programming Chapter Chapter
CSCE Introduction to Program Design and Concepts J. Michael Moore Spring 2015 Set 17: Vectors and Arrays 1 Based on slides created by Bjarne Stroustrup.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
Chapter 18 Vectors and Arrays Bjarne Stroustrup
POINTERS AND MEMORY ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Dynamic Storage Allocation
Chapter 18 Vectors and Arrays
Dynamic Memory CSCE 121 J. Michael Moore.
CSC 253 Lecture 8.
CSC 253 Lecture 8.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
Chapter 17 vector and Free Store
Pointers And Memory Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
Chapter 12 Pointers and Memory Management
Dynamic Memory CSCE 121.
Pointers, Dynamic Data, and Reference Types
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Chapter 17 vector and Free Store Bjarne Stroustrup

Abstract Vector is not just the most useful standard container, it is also provides examples of some of the most important/powerful/ interesting implementation techniques. In this and the following lectures, we go through a series of increasingly sophisticated vector implementations, seeing classical problems related to use of memory and providing solutions. Here, we discuss free store (heap storage) management, and pointers. Vector is not just the most useful standard container, it is also provides examples of some of the most important/powerful/ interesting implementation techniques. In this and the following lectures, we go through a series of increasingly sophisticated vector implementations, seeing classical problems related to use of memory and providing solutions. Here, we discuss free store (heap storage) management, and pointers. 2Stroustrup/PPP Oct'11

Overview Vector revisited Vector revisited How are they implemented? How are they implemented? Pointers and free store Pointers and free store Allocation (new) Allocation (new) Access Access Arrays and subscripting: [] Arrays and subscripting: [] Dereferencing: * Dereferencing: * Deallocation (delete) Deallocation (delete) Destructors Destructors Copy constructor and copy assignment Copy constructor and copy assignment Arrays Arrays Array and pointer problems Array and pointer problems Changing size Changing size Templates Templates Range checking and exceptions Range checking and exceptions 3Stroustrup/PPP Oct'11

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

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

Building from the ground up At the lowest level, close to the hardware, lifes simple and brutal At the lowest level, close to the hardware, lifes simple and brutal You have to program everything yourself You have to program everything yourself You have no type checking to help you You have no type checking to help you Run-time errors are found when data is corrupted or the program crashes 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 We want to get to a higher level as quickly as we can To become productive and reliable To become productive and reliable To use a language fit for humans To use a language fit for humans Chapter basically shows all the steps needed Chapter basically shows all the steps needed The alternative to understanding is to believe in magic 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 The techniques for building vector are the ones underlying all higher-level work with data structures 6Stroustrup/PPP Oct'11

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

Vector // a very simplified vector of doubles (like vector ): 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 * means pointer to so double* is a pointer to double What is a pointer? What is a pointer? how do we make a pointer point to elements? how do we make a pointer point to elements? How do we allocate elements? How do we allocate elements? 8Stroustrup/PPP Oct'11

Pointer values Pointer values are memory addresses Pointer values are memory addresses Think of them as a kind of integer values Think of them as a kind of integer values The first byte of memory is 0, the next 1, and so on The first byte of memory is 0, the next 1, and so on A pointer p can hold the address of a memory location A pointer p can hold the address of a memory location ^ p A pointer points to an object of a given type A pointer points to an object of a given type E.g. a double* points to a double, not to a string E.g. a double* points to a double, not to a string A pointers type determines how the memory referred to by the pointers value is used A pointers type determines how the memory referred to by the pointers value is used E.g. what a double* points to can be added not, say, concatenated E.g. what a double* points to can be added not, say, concatenated Stroustrup/PPP Oct'11

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 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) 10 Free store: 4 new allocates memory from the free store and returns a pointer to the allocated memory A pointer sz:elem: Stroustrup/PPP Oct'11

The computers memory As a program sees it As a program sees it Local variables lives on the stack Local variables lives on the stack Global variables are static data Global variables are static data The executable code are in the code section The executable code are in the code section 11Stroustrup/PPP Oct'11

The free store (sometimes called "the heap") You request memory "to be allocated" "on the free store" by the new operator You request memory "to be allocated" "on the free store" by the new operator The new operator returns a pointer to the allocated memory The new operator returns a pointer to the allocated memory A pointer is the address of the first byte of the memory A pointer is the address of the first byte of the memory For example For example int* p = new int;// allocate one uninitialized int // int* means pointer to int int* p = new int;// allocate one uninitialized int // int* means pointer to int int* q = new int[7];// allocate seven uninitialized ints // an array of 7 ints int* q = new int[7];// allocate seven uninitialized ints // an array of 7 ints double* pd = new double[n];// allocate n uninitialized doubles double* pd = new double[n];// allocate n uninitialized doubles A pointer points to an object of its specified type A pointer points to an object of its specified type A pointer does not know how many elements it points to A pointer does not know how many elements it points to 12 p: q: Stroustrup/PPP Oct'11

Access Individual elements Individual elements int* p1 = new int;// get (allocate) a new uninitialized int int* p2 = new int(5);// get a new int initialized to 5 int x = *p2;// get/read the value pointed to by p2 // (or get the contents of what p2 points to) // in this case, the integer 5 int y = *p1;// undefined: y gets an undefined value; dont do that 13 5 p2: ??? p1: Stroustrup/PPP Oct'11

Access Arrays (sequences of elements) Arrays (sequences of elements) int* p3 = new int[5];// get (allocate) 5 ints // array elements are numbered 0, 1, 2, … p3[0] = 7;// write to (set) the 1 st element of p3 p3[1] = 9; int x2 = p3[1];// get the value of the 2 nd element of p3 int x3 = *p3; // we can also use the dereference operator * for an array // *p3 means p3[0] (and vice versa) // *p3 means p3[0] (and vice versa) p3: Stroustrup/PPP Oct'11

Why use free store? To allocate objects that have to outlive the function that creates them: To allocate objects that have to outlive the function that creates them: For example For example double* make(int n)// allocate n ints { return new double[n]; } Another example: vector's constructor Another example: vector's constructor 15Stroustrup/PPP Oct'11

Pointer values Pointer values are memory addresses Pointer values are memory addresses Think of them as a kind of integer values Think of them as a kind of integer values The first byte of memory is 0, the next 1, and so on The first byte of memory is 0, the next 1, and so on 16 // you can see pointer value (but you rarely need/want to): char* p1 = new char('c');// allocate a char and initialize it to 'c' int* p2 = new int(7);// allocate an int and initialize it to 7 cout << "p1==" << p1 << " *p1==" << *p1 << "\n"; // p1==??? *p1==c cout << "p2==" << p2 << " *p2==" << *p2 << "\n"; // p2==??? *p2=7 0122^ p2*p2 Stroustrup/PPP Oct'11

Access A pointer does not know the number of elements that it's pointing to (only the address of the first element) A pointer does not know the number of elements that it's pointing to (only the address of the first element) double* p1 = new double; *p1 = 7.3;// ok p1[0] = 8.2;// ok p1[17] = 9.4;// ouch! Undetected error p1[-4] = 2.4;// ouch! Another undetected error double* p2 = new double[100]; *p2 = 7.3;// ok p2[17] = 9.4;// ok p2[-4] = 2.4;// ouch! Undetected error p1: p2: Stroustrup/PPP Oct'11

Access A pointer does not know the number of elements that it's pointing to A pointer does not know the number of elements that it's pointing to double* p1 = new double; double* p2 = new double[100]; p1[17] = 9.4; // error (obviously) p1 = p2; // assign the value of p2 to p1 p1[17] = 9.4; // now ok: p1 now points to the array of 100 doubles 18 p1: p2: p1: (after the assignment) [0]:[99]: Stroustrup/PPP Oct'11

Access A pointer does know the type of the object that it's pointing to A pointer does know the type of the object that it's pointing to int* pi1 = new int(7); int* pi2 = pi1;// ok: pi2 points to the same object as pi1 double* pd = pi1;// error: can't assign an int* to a double* char* pc = pi1;// error: can't assign an int* to a char* There are no implicit conversions between a pointer to one value type to a pointer to another value type There are no implicit conversions between a pointer to one value type to a pointer to another value type However, there are implicit conversions between value types: However, there are implicit conversions between value types: *pc = 8;// ok: we can assign an int to a char *pc = *pi1;// ok: we can assign an int to a char pi1: pc: Stroustrup/PPP Oct'11

Pointers, arrays, and vector Note 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 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 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). 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). 20Stroustrup/PPP Oct'11

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) const { 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) << ' '; } Stroustrup/PPP Oct'11

A problem: memory leak double* calc(int result_size, int max) { double* p = new double[max];// allocate another max doubles // i.e., get max doubles from the free store double* result = new double[result_size]; // … use p to calculate results to be put in result … return result; } double* r = calc(200,100);// oops! We forgot to give the memory // allocated for p back to the free store Lack of de-allocation (usually called "memory leaks") can be a serious problem in real-world programs Lack of de-allocation (usually called "memory leaks") can be a serious problem in real-world programs A program that must run for a long time can't afford any memory leaks A program that must run for a long time can't afford any memory leaks 22Stroustrup/PPP Oct'11

A problem: memory leak double* calc(int result_size, int max) { int* p = new double[max]; // allocate another max doubles // i.e., get max doubles from the free store double* result = new double[result_size]; // … use p to calculate results to be put in result … delete[ ] p;// de-allocate (free) that array // i.e., give the array back to the free store return result; } double* r = calc(200,100); // use r delete[ ] r;// easy to forget 23Stroustrup/PPP Oct'11

Memory leaks A program that needs to run "forever" can't afford any memory leaks A program that needs to run "forever" can't afford any memory leaks An operating system is an example of a program that runs forever An operating system is an example of a program that runs forever If a function leaks 8 bytes every time it is called, how many days can it run before it has leaked/lost a megabyte? If a function leaks 8 bytes every time it is called, how many days can it run before it has leaked/lost a megabyte? Trick question: not enough data to answer, but about 130,000 calls Trick question: not enough data to answer, but about 130,000 calls All memory is returned to the system at the end of the program All memory is returned to the system at the end of the program If you run using an operating system (Windows, Unix, whatever) If you run using an operating system (Windows, Unix, whatever) Program that runs to completion with predictable memory usage may leak without causing problems Program that runs to completion with predictable memory usage may leak without causing problems i.e., memory leaks arent good/bad but they can be a major problem in specific circumstances i.e., memory leaks arent good/bad but they can be a major problem in specific circumstances 24Stroustrup/PPP Oct'11

Memory leaks Another way to get a memory leak Another way to get a memory leak void f() { double* p = new double[27]; // … p = new double[42]; // … delete[] p; } // 1 st array (of 27 doubles) leaked 25 p: 2 nd value 1 st value Stroustrup/PPP Oct'11

Memory leaks How do we systematically and simply avoid memory leaks? How do we systematically and simply avoid memory leaks? don't mess directly with new and delete don't mess directly with new and delete Use vector, etc. Use vector, etc. Or use a garbage collector Or use a garbage collector A garbage collector is a program the keeps track of all of your allocations and returns unused free-store allocated memory to the free store (not covered in this course; see A garbage collector is a program the keeps track of all of your allocations and returns unused free-store allocated memory to the free store (not covered in this course; see Unfortunately, even a garbage collector doesnt prevent all leaks Unfortunately, even a garbage collector doesnt prevent all leaks See also Chapter 25 See also Chapter 25 26Stroustrup/PPP Oct'11

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) } 27Stroustrup/PPP Oct'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: Note: this is an example of a general and important technique: acquire resources in a constructor acquire resources in a constructor release them in the destructor release them in the destructor Examples of resources: memory, files, locks, threads, sockets Examples of resources: memory, files, locks, threads, sockets 28Stroustrup/PPP Oct'11

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 The delete now looks verbose and ugly How do we avoid forgetting to delete[ ] p? How do we avoid forgetting to delete[ ] p? Experience shows that we often forget Experience shows that we often forget Prefer deletes in destructors Prefer deletes in destructors 29Stroustrup/PPP Oct'11

Free store summary Allocate using new Allocate using new New allocates an object on the free store, sometimes initializes it, and returns a pointer to it New allocates an object on the free store, sometimes initializes it, and returns a pointer to it int* pi = new int;// default initialization (none for int) int* pi = new int;// default initialization (none for int) char* pc = new char('a');// explicit initialization char* pc = new char('a');// explicit initialization double* pd = new double[10];// allocation of (uninitialized) array double* pd = new double[10];// allocation of (uninitialized) array New throws a bad_alloc exception if it can't allocate New throws a bad_alloc exception if it can't allocate Deallocate using delete and delete[ ] Deallocate using delete and delete[ ] delete and delete[ ] return the memory of an object allocated by new to the free store so that the free store can use it for new allocations delete and delete[ ] return the memory of an object allocated by new to the free store so that the free store can use it for new allocations delete pi;// deallocate an individual object delete pi;// deallocate an individual object delete pc;// deallocate an individual object delete pc;// deallocate an individual object delete[ ] pd;// deallocate an array delete[ ] pd;// deallocate an array Delete of a zero-valued pointer ("the null pointer") does nothing Delete of a zero-valued pointer ("the null pointer") does nothing char* p = 0; char* p = 0; delete p;// harmless delete p;// harmless 30Stroustrup/PPP Oct'11

void* void* means pointer to some memory that the compiler doesn't know the type of void* means pointer to some memory that the compiler doesn't know the type of We use void* when we want to transmit an address between pieces of code that really don't know each other's types – so the programmer has to know We use void* when we want to transmit an address between pieces of code that really don't know each other's types – so the programmer has to know Example: the arguments of a callback function Example: the arguments of a callback function There are no objects of type void There are no objects of type void void v;// error void v;// error void f();// f() returns nothing – f() does not return an object of type void void f();// f() returns nothing – f() does not return an object of type void Any pointer to object can be assigned to a void* Any pointer to object can be assigned to a void* int* pi = new int; int* pi = new int; double* pd = new double[10]; double* pd = new double[10]; void* pv1 = pi; void* pv1 = pi; void* pv2 = pd; void* pv2 = pd; 31Stroustrup/PPP Oct'11

void* To use a void* we must tell the compiler what it points to To use a void* we must tell the compiler what it points to void f(void* pv) { void* pv2 = pv;// copying is ok (copying is what void*s are for) double* pd = pv;// error: cant implicitly convert void* to double* *pv = 7;// error: you cant dereference a void* // good! The int 7 is not represented like the double 7.0) pv[2] = 9;// error: you cant subscript a void* pv++;// error: you cant increment a void* int* pi = static_cast (pv);// ok: explicit conversion // … } A static_cast can be used to explicitly convert to a pointer to object type A static_cast can be used to explicitly convert to a pointer to object type "static_cast" is a deliberately ugly name for an ugly (and dangerous) operation – use it only when absolutely necessary "static_cast" is a deliberately ugly name for an ugly (and dangerous) operation – use it only when absolutely necessary 32Stroustrup/PPP Oct'11

void* void* is the closest C++ has to a plain machine address void* is the closest C++ has to a plain machine address Some system facilities require a void* Some system facilities require a void* Remember FLTK callbacks? Remember FLTK callbacks? Address is a void*: Address is a void*: typedef void* Address; void Lines_window::cb_next(Address,Address) 33Stroustrup/PPP Oct'11

Pointers and references Think of a reference as an automatically dereferenced pointer Think of a reference as an automatically dereferenced pointer Or as an alternative name for an object Or as an alternative name for an object A reference must be initialized A reference must be initialized The value of a reference cannot be changed after initialization The value of a reference cannot be changed after initialization int x = 7; int y = 8; int* p = &x;*p = 9; p = &y;// ok int& r = x;x = 10; r = &y;// error (and so is all other attempts to change what r refers to) 34Stroustrup/PPP Oct'11

Next lecture The next lecture discusses copying and arrays The next lecture discusses copying and arrays 35Stroustrup/PPP Oct'11