WHAT POINTERS REALLY ARE

Similar documents
ALL ABOUT POINTERS C/C++ POINTERS

a data type is Types

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

Arrays, Pointers and Memory Management

Pointers, Dynamic Data, and Reference Types

Vector and Free Store (Vectors and Arrays)

nptr = new int; // assigns valid address_of_int value to nptr std::cin >> n; // assigns valid int value to n

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Dynamic Allocation in C

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

CSC1322 Object-Oriented Programming Concepts

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Pointers and Memory 1

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2

11. Arrays. For example, an array containing 5 integer values of type int called foo could be represented as:

Complex data types Structures Defined types Structures and functions Structures and pointers (Very) brief introduction to the STL

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

Memory, Arrays & Pointers

Kurt Schmidt. October 30, 2018

Chapter 2: Basic Elements of C++

CSE 333 Midterm Exam 2/14/14

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Procedural Programming & Fundamentals of Programming

Type Aliases. Examples: using newtype = existingtype; // C++11 typedef existingtype newtype; // equivalent, still works

CS 31: Intro to Systems Arrays, Structs, Strings, and Pointers. Kevin Webb Swarthmore College March 1, 2016

Computer Science II Lecture 1 Introduction and Background

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

Fast Introduction to Object Oriented Programming and C++

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

377 Student Guide to C++

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

6.096 Introduction to C++ January (IAP) 2009

Objectives. In this chapter, you will:

CS 11 C track: lecture 5

Chap 0: Overview. Overview of basic C++ syntax Refresh programming basics C++ Vs. Java differences Coding conventions used. EECS 268 Programming II 1

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

C Syntax Arrays and Loops Math Strings Structures Pointers File I/O. Final Review CS Prof. Jonathan Ventura. Prof. Jonathan Ventura Final Review

SYSC 2006 C Winter String Processing in C. D.L. Bailey, Systems and Computer Engineering, Carleton University

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

Memory and C++ Pointers

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Fall 2018 Discussion 2: September 3, 2018

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

CS 31 Review Sheet. Tau Beta Pi - Boelter Basics 2. 2 Working with Decimals 2. 4 Operators 3. 6 Constants 3.

Introduction to Programming Using Java (98-388)

CS201 Some Important Definitions

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:


Short Notes of CS201

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

PRINCIPLES OF OPERATING SYSTEMS

Lectures 5-6: Introduction to C

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

Pointers and References

CS201 - Introduction to Programming Glossary By

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Creating a C++ Program

Engineering Problem Solving with C++, Etter

A brief introduction to C programming for Java programmers

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

CS2141 Software Development using C/C++ C++ Basics

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

Principles of Programming Pointers, Dynamic Memory Allocation, Character Arrays, and Buffer Overruns

6. User Defined Functions I

UEE1302 (1102) F10 Introduction to Computers and Programming (I)

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

Dynamic Allocation in C

CSE 333 Autumn 2013 Midterm

Name. CPTR246 Spring '17 (100 total points) Exam 2

Variables and literals

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

So far, system calls have had easy syntax. Integer, character string, and structure arguments.

Object-Oriented Programming

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

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

C Pointers. 6th April 2017 Giulio Picierro

CS24 Week 3 Lecture 1

Types, Values, Variables & Assignment. EECS 211 Winter 2018

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

Introduction to Programming using C++

12. Pointers Address-of operator (&)

Lectures 5-6: Introduction to C

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012

pointers + memory double x; string a; int x; main overhead int y; main overhead

Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172. In compensation, no class on Friday, Jan. 31.

8. The C++ language, 1. Programming and Algorithms II Degree in Bioinformatics Fall 2017

Linked Lists CS 16: Solving Problems with Computers I Lecture #16

Chapter 2. Procedural Programming

Intro. Scheme Basics. scm> 5 5. scm>

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

CMSC 202 Midterm Exam 1 Fall 2015

Transcription:

POINTERS What is a pointer? The index of a book contains pointers. A URL (e.g., http://turing.ubishops.ca/home/cs318) is a pointer. A street address is a pointer. What is then a forwarding address? a pointer to a pointer! OK, so what is a (C++) pointer? Computer memory contains data which can be accessed using an address. A pointer is such an address, nothing more. If you want, computer memory is like an array holding data. A pointer then is an index in such an array. What are in fact pointers? POINTERS What is a pointer? The index of a book contains pointers. A URL (e.g., http://turing.ubishops.ca/home/csc218) is a pointer. A street address is a pointer. What is then a forwarding address? a pointer to a pointer! OK, so what is a (C++) pointer? Computer memory contains data which can be accessed using an address. A pointer is such an address, nothing more. If you want, computer memory is like an array holding data. A pointer then is an index in such an array. What are in fact pointers? ARRAYS, POINTERS, STRUCTURES/1 ARRAYS, POINTERS, STRUCTURES/1 POINTERS (CONT D) WHAT POINTERS REALLY ARE Pointers can (just as array indices) be stored in variables. If we have some type d, then d vx; vx is a variable of type d d* px; px is a (variable holding a) pointer to a variable of type d &vx *px denotes the address of vx (i.e., a pointer, of type d*) denotes the value from the memory location pointed at by px, of type d (we thus dereference px) 10 int x = 10; int* px = &x; int** ppx = &px; x px 7830 ppx 8991 7830 8991 9001 (&x) (&px) (&ppx) *px = x + 1; cout << x; 11 Since a pointer is an address, it is usually represented internally as unsigned int. Do we need a type for a pointer? Why? Always? int x=10; void* p = &x; int * pi; float* pf; pi = (int*)p; pf = (float*)p; cout << "Pointer " << p << " holds the int: "<< *pi << "...and the float: " << *pf << "\n"; Special pointer (of type void*): NULL (really, 0), which points to nothing. ARRAYS, POINTERS, STRUCTURES/2 ARRAYS, POINTERS, STRUCTURES/3

WHAT POINTERS REALLY ARE Since a pointer is an address, it is usually represented internally as unsigned int. Do we need a type for a pointer? Why? Always? POINTER ARITHMETIC The types of pointers do matter: 1. We know what we get when we dereference a pointer 2. We can do meaningful pointer arithmetic int i=10; long j=10; int *x = &i; long *y = &j; int *x1 = x + 3; long *y1 = y + 3; int *x2 = x - 2; long *y2 = y - 2; int x=10; void* p = &x; int * pi; float* pf; pi = (int*)p; pf = (float*)p; cout << "Pointer " << p << " holds the int: "<< *pi << "...and the float: " << *pf << "\n"; 0x34791B 0x34791C 0x34791D x 2 x x+3 0x34791E 0x34791F 0x347920 0x347921 0x347922 0x347923 0x347924 0x437925 0x437926 0x437927 Special pointer (of type void*): NULL (really, 0), which points to nothing. Meaningful pointer arithmetic?!? ARRAYS, POINTERS, STRUCTURES/3 ARRAYS, POINTERS, STRUCTURES/4 POINTER ARITHMETIC POINTER ARITHMETIC The types of pointers do matter: 1. We know what we get when we dereference a pointer 2. We can do meaningful pointer arithmetic int i=10; long j=10; int *x = &i; long *y = &j; int *x1 = x + 3; long *y1 = y + 3; int *x2 = x - 2; long *y2 = y - 2; x 2 x y x+3 The types of pointers do matter: 1. We know what we get when we dereference a pointer 2. We can do meaningful pointer arithmetic int i=10; long j=10; int *x = &i; long *y = &j; int *x1 = x + 3; long *y1 = y + 3; int *x2 = x - 2; long *y2 = y - 2; y 2 x 2 x y x+3 y+3 0x437927 0x437926 0x437925 0x347924 0x347923 0x347922 0x347921 0x347920 0x34791F 0x34791E 0x34791D 0x34791C 0x34791B 0x437927 0x437926 0x437925 0x347924 0x347923 0x347922 0x347921 0x347920 0x34791F 0x34791E 0x34791D 0x34791C 0x34791B Meaningful pointer arithmetic?!? Meaningful pointer arithmetic?!? ARRAYS, POINTERS, STRUCTURES/4 ARRAYS, POINTERS, STRUCTURES/4

ARRAYS AND POINTERS An array is just a pointer to its content: float nums[6] = {1,2,3 nums 72466 10201 nums[0] 72466 nums[1] nums[2] 1.0 2.0 3.0 nums[3] nums[4] nums[5] 72467 72468 72469 72470 72471 In addition, when you declare an array (contiguous) memory space is also reserved to hold its elements. What do they all mean? float nums[6] = {1,2,3; int nums[6] = {1,2,3; float* p1 = nums; int* p1 = nums; float* p2 = nums + 3; int* p2 = nums + 3; ARRAYS VERSUS POINTERS The following declarations mean almost the same thing: int* numsp; int numsa[20]; Because we have: numsa[2] = 17; Good numsp[2] = 17; Disaster! Prize for the most uninformative error message goes to Segmentation fault. But it is perfectly good to do: int numsp[] = {1,2,3; In other words, you do not have to provide the dimension for an array if you initialize it at the moment of declaration (e.g., by providing a literal array). ARRAYS, POINTERS, STRUCTURES/5 ARRAYS, POINTERS, STRUCTURES/6 ARRAY SUBSCRIPTS ARRAYS, POINTERS, AND FUNCTIONS We access elements in an array precisely as we do it in Java: cout << x[6]; prints the seventh element of x x[5] = 20; assigns 20 to the sixth element of x The subscript operator [] is in fact implemented usingpointer arithmetic x[5] is a shorthand for (and thus a perfect equivalent to) &x+5. the subscript operator works with any pointer, not just with arrays. it does correct pointer arithmetic so that we access the intended element y 2 y y+3 x[ 2] x[3] y[ 2] x 2 x x+3 y[3] 0x34791B 0x34791C 0x34791D 0x34791E 0x34791F 0x347920 0x347921 0x347922 0x347923 0x347924 0x437925 0x437926 0x437927 #include <iostream> using namespace std; void translate(char a) { if (a == A ) a = 5 ; else a = 0 ; void translate(char* array, int size) { for (int i = 0; i < size; i++) { if (array[i] == A ) array[i] = 5 ; else array[i] = 0 ; char mark = A ; char marks[5] = { A, F, A, F, F ; translate(mark); translate(marks,5); cout << mark << "\n"; for (int i = 0; i < 5; i++) cout << marks[i] << " "; cout << "\n"; A 5 0 5 0 0 ARRAYS, POINTERS, STRUCTURES/7 ARRAYS, POINTERS, STRUCTURES/8

ARRAYS, POINTERS, AND FUNCTIONS #include <iostream> using namespace std; void translate(char a) { // translate, by the way, is a OVERLOADED FUNCTION if (a == A ) a = 5 ; else a = 0 ; void translate(char* array, int size) { for (int i = 0; i < size; i++) { if (array[i] == A ) array[i] = 5 ; else array[i] = 0 ; ARRAYS, POINTERS, AND FUNCTIONS (CONT D) #include <iostream> using namespace std; int translate(char a) { // still overloaded... if (a == A ) a = 5 ; else a = 0 ; return a; void translate(char* array, int size) { for (int i = 0; i < size; i++) { if (array[i] == A ) array[i] = 5 ; else array[i] = 0 ; char mark = A ; char marks[5] = { A, F, A, F, F ; translate(mark); translate(marks,5); cout << mark << "\n"; for (int i = 0; i < 5; i++) cout << marks[i] << " "; cout << "\n"; A 5 0 5 0 0 char mark = A ; char marks[5] = { A, F, A, F, F ; mark = translate(mark); translate(marks,5); cout << mark << "\n"; for (int i = 0; i < 5; i++) cout << marks[i] << " "; cout << "\n"; 5 5 0 5 0 0 ARRAYS, POINTERS, STRUCTURES/8 ARRAYS, POINTERS, STRUCTURES/9 POINTERS AND FUNCTIONS CALL BY REFERENCE An argument can be passed in C++ to a function using: Call by value: the value of the argument is passed; argument cannot be changed by the function. int afunction(int i); Call by reference: the pointer to the argument is passed to the function; argument can be changed at will by the function. int afunction(int* i); Used for output arguments (messy, error prone syntax). Call by constant reference: the pointer to the argument is passed to the function; but the function is not allowed to change the argument. int afunction(const int* i); more useful: int afunction(const char* i); Used for bulky arguments (still messy syntax). foo.cc void increment (int* i) { *i = *i + 1; void increment1 (const int* i) { *i = *i + 1; int n = 0; increment(&n); increment1(&n); g++ -Wall foo.cc foo.cc: In function void increment1(const int *) : foo.cc:9: assignment of read-only location ARRAYS, POINTERS, STRUCTURES/10 ARRAYS, POINTERS, STRUCTURES/11

CALL BY REFERENCE (CONT D) CALL BY REFERENCE (CONT D) foo.cc void increment (int& i) { i = i + 1; void increment1 (const int& i) { i = i + 1; int n = 0; increment(n); increment1(n); g++ -Wall foo.cc no more messy syntax! foo.cc: In function void increment1(const int &) : foo.cc:9: assignment of read-only reference i foo.cc #include <iostream> using namespace std; void increment (int& i) { i = i + 1; int increment1 (const int& i) { int r = i + 1; return r; int n = 0; increment(n); cout << n << "\n"; n = increment1(n); cout << n << "\n"; output 1 2 ARRAYS, POINTERS, STRUCTURES/12 ARRAYS, POINTERS, STRUCTURES/13 CALLING CONVENTIONS IN C++ AND JAVA C STRINGS The following are the implicit calling conventions: What Java C++ Primitive types value value (int, float, etc.) Arrays reference value Objects reference value In C++ everything is passed by value unless explicitly stated otherwise. Arrays are apparently passed by reference, but only because of the array structure (pointer + content). In Java there is no other way to pass arguments than the implicit one. In C++ you can request that an argument be passed by reference by either passing a pointer to the actual argument or by saying explicitly that you want to pass the argument by reference. There is no special type for strings. Instead, strings are simply arrays of characters. Literal strings can be written surrounded by double quotes though. char message[20] = "Hello."; The last character in a string is always the null byte ( \0 ). So if you declare a string of size 20 it will hold a maximum of 19 characters. C does not check for array overflow, so be careful not to go over the array size. You can access individual characters just as you access elements in a normal array: message[1] = x ; Strings cannot be compared using the usual comparison operators (e.g., ==) (why?). Use strcmp instead. ARRAYS, POINTERS, STRUCTURES/14 ARRAYS, POINTERS, STRUCTURES/15

OPERATIONS ON STRINGS STRUCTURES You can implement your own operations on strings (just do not forget about the null byte at the end). Some operations are already defined for you though, including: Copy a string: strcpy (see man strcpy) Length of a string: strlen (see man strlen) Just do not forget to include the appropriate header: #include <string.h> An array holds a number of elements of a given type. Individual elements are referred to by integer indices. By contrast, a structure holds elements of not necessarily the same type. Individual elements are referred to by symbolic names. Of course, we cannot thus loop over the members of a structure. For instance, a structure representing a student might contain the given name and surname (strings), the student number (integer), the mailbox number (integer), and the grade point average (floating point). ARRAYS, POINTERS, STRUCTURES/16 ARRAYS, POINTERS, STRUCTURES/17 STUDENT STRUCTURE STUDENT STRUCTURE struct student { char* name; char* surname; unsigned int number; unsigned short mailbox; float gpa; ; struct student { char* name; char* surname; unsigned int number; unsigned short mailbox; float gpa; ; student studs[5]; studs[0].name = "Jane"; studs[0].surname = "Doe"; studs[0].number = 1234567; studs[1].name = "John"; studs[1].surname = "Smith"; studs[1].number = 7654321; cout << studs[1].name << " " << studs[1].surname << " (" << studs[1].number << ")\n"; student studs[5]; studs[0].name = "Jane"; studs[0].surname = "Doe"; studs[0].number = 1234567; studs[1].name = "John"; studs[1].surname = "Smith"; studs[1].number = 7654321; cout << studs[1].name << " " << studs[1].surname << " (" << studs[1].number << ")\n"; ARRAYS, POINTERS, STRUCTURES/18 ARRAYS, POINTERS, STRUCTURES/18

POINTERS TO STRUCTURES Let s do something useful: a (linked) list (of integers). nil list car cdr list data car cdr Nothing! data car cdr cons_cell data Nothing! Interesting operations: Operation Meaning cons adds an integer to the list car returns the first element of a list cdr returns a list without its first element null returns true iff the list is empty LINKED LIST struct cons_cell { int car; cons_cell* cdr; ; typedef cons_cell* list; const list nil = 0; // careful, could be bad programming practice! int null (list cons) { return cons == nil; list cons (int car, list cdr = nil) { list new_cons = new cons_cell; new_cons -> car = car; // (*new_cons).car = car; new_cons -> cdr = cdr; // (*new_cons).cdr = cdr; return new_cons; int car (list cons) { return cons -> car; list cdr (list cons) { return cons -> cdr; ARRAYS, POINTERS, STRUCTURES/19 ARRAYS, POINTERS, STRUCTURES/20 NEW (AND DELETE) DYNAMIC MEMORY MANAGEMENT new allocates memory for your data. The following are (somehow) equivalent: char message[256]; Exception: char* pmessage; pmessage = new char[256]; message takes care of itself (i.e., gets deleted when it is no longer in use), whereas pmessage however must be explicitly deleted when it is no longer needed: delete[] pmessage; Perrils of not using new: list cons (int car, list cdr = nil) { cons_cell new_cons; new_cons.car = car; new_cons.cdr = cdr; return &new_cons; list bad = cons(1); cout << car(bad); Boom! int foo () { cons_cell c; list l = new cons_cell; Heap Conclusion: cons_cell foo returns l zap!! int i; foo(); foo returns c Stack main: i foo: c zap!! int cons_cell ARRAYS, POINTERS, STRUCTURES/21 ARRAYS, POINTERS, STRUCTURES/22

USING LINKED LISTS USING LINKED LISTS (CONT D) list rmth (list cons, int which) { list place = cons; for (int i = 0; i < which - 1; i++) { if (null(place)) break; place = place -> cdr; if (! null(place) ) { if (null(cdr(place))) place -> cdr = nil; else place -> cdr = cdr(place -> cdr); return cons; int elm = -1; list lst = nil; while (elm!= 0) { cin >> elm; if (elm!= 0) lst = cons(elm,lst); lst = rmth(lst,1); lst = rmth(lst,10); cout << "List is:\n"; list iter = lst; while (! null(iter) ) { cout << car(iter) << "\n"; iter = cdr(iter); 1 2 3 4 5 0 List is: 5 3 2 1 ARRAYS, POINTERS, STRUCTURES/23 ARRAYS, POINTERS, STRUCTURES/24 MEMORY LEAKS SAY NO TO MEMORY LEAKS A good example: list rmth (list cons, int which) { list place = cons; for (int i = 0; i < which - 1; i++) { if (null(place)) break; place = place -> cdr; if (! null(place) ) { if (null(cdr(place))) place -> cdr = nil; else place -> cdr = cdr(place -> cdr); return cons; If you create something using new then you must eventually delete it using delete. list rmth (list cons, int which) { list place = cons; for (int i = 0; i < which - 1; i++) { if (null(place)) break; place = place -> cdr; if (! null(place) ) { if (null(cdr(place))) place -> cdr = nil; else { list to_delete = cdr(place); place -> cdr = cdr(place -> cdr); delete to_delete; return cons; ARRAYS, POINTERS, STRUCTURES/25 ARRAYS, POINTERS, STRUCTURES/26

STUDENT STRUCTURE, TAKE TWO The following won t work. Why? What would happen if it would work? struct student { char name[20]; char surname[20]; unsigned int number; unsigned short mailbox; float gpa; ; student studs[5]; studs[0].name = "Jane"; studs[0].surname = "Doe"; studs[0].number = 1234567; studs[1].name = "John"; studs[1].surname = "Smith"; studs[1].number = 7654321; cout << studs[1].name << " " << studs[1].surname << " (" << studs[1].number << ")\n"; STUDENT STRUCTURE, TAKE TWO (CONT D) The following does work. struct student { char name[20]; char surname[20]; unsigned int number; unsigned short mailbox; float gpa; ; student studs[5]; strncpy(studs[0].name,"jane",20); strncpy(studs[0].surname,"doe",20); studs[0].number = 1234567; strncpy(studs[1].name,"john",20); strncpy(studs[1].surname,"smith",20); studs[1].number = 7654321; cout << studs[1].name << " " << studs[1].surname << " (" << studs[1].number << ")\n"; ARRAYS, POINTERS, STRUCTURES/27 ARRAYS, POINTERS, STRUCTURES/28 THE PERILS OF DELETE THE PERILS OF DELETE Thou shall not leak memory, but also: Thou shall not leak memory, but also: Thou shall not leave stale pointers behind. char* str = new char[128]; strcpy(str,"hello"); char* p = str; delete p; allocate memory for str put something in there ( hello ) p points to the same thing hello is gone, str is a stale pointer!! Thou shall not leave stale pointers behind. char* str = new char[128]; strcpy(str,"hello"); char* p = str; delete p; allocate memory for str put something in there ( hello ) p points to the same thing hello is gone, str is a stale pointer!! Thou shall not dereference deleted pointers. strcpy(str,"hi"); Thou shall not delete a pointer more than once. delete str; str already deleted!! str already deleted!! You can however delete null pointers as many times as you wish! So assign zero to deleted pointers whenever possible (not a panaceum) Thou shall not dereference deleted pointers. strcpy(str,"hi"); Thou shall not delete a pointer more than once. delete str; str already deleted!! str already deleted!! You can however delete null pointers as many times as you wish! So assign zero to deleted pointers whenever possible (not a panaceum) ARRAYS, POINTERS, STRUCTURES/29 ARRAYS, POINTERS, STRUCTURES/29

THE PERILS OF DELETE (CONT D) struct prof { char* name; char* dept; ; char *csc = new char[30]; strcpy (csc,"computer Science"); prof *stefan, *dimitri, *bruda; stefan = new prof; dimitri = new prof; stefan >name = new char[30]; dimitri >name = new char[30]; strcpy(stefan >name,"stefan Bruda"); strcpy(dimitri >name,"dimitri Vouliouris"); stefan >dept = csc; dimitri >dept = csc; // Delete dimitri delete dimitri >name; delete dimitri >dept; delete dimitri; Exogenous data OK??? Indigenous data // Copy stefan bruda = new prof; // (a) Shallow copying bruda >name = stefan >name; bruda >dept = stefan >dept; // Can we delete stefan now?? // (b) Deep copying bruda >name = new char[30]; bruda >dept = new char[30]; strcpy(bruda.name,stefan.name); strcpy(bruda.dept,stefan.dept); // Can we delete stefan now?? ARRAYS, POINTERS, STRUCTURES/30