Pointers by Dr. Bun Yue Professor of Computer Science 2013 CSCI 3333 Data Structures.

Slides:



Advertisements
Similar presentations
1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs.
Advertisements

Introduction to C++ Programming. Brief Facts About C++ Evolved from C Designed and implemented by Bjarne Stroustrup at the Bell Labs in the early 1980s.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
Pointer, malloc and realloc 1. Name entered was 6 char, not enough space to put null terminator 2 Array of char.
DYNAMIC MEMORY MANAGEMENT. int x; When the source code containing this statement is compiled and linked, an executable file is generated. When the executable.
Pointers Typedef Pointer Arithmetic Pointers and 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.
Chapter 10.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
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++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
1 Today’s Objectives  Announcements Turn in Homework #1 Homework #2 is posted and it is due on 21-Jun  Review Quiz #1  Pointers and C-style strings.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
February 11, 2005 More Pointers Dynamic Memory Allocation.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
Rossella Lau Lecture 1, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 1: Introduction What this course is about:
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
1 C++ Syntax and Semantics, and the Program Development Process.
1 Pointers Arrays have a disadvantage: Their size must be known at compile time. We would like the capability to allocate an array-like object of any needed.
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
Pointers, Variables, and Memory. Variables and Pointers When you declare a variable, memory is allocated to store a value. A pointer can be used to hold.
1 Data Structures CSCI 132, Spring 2014 Lecture 10 Dynamic Memory and Pointers.
C++ Memory Overview 4 major memory segments Key differences from Java
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
Dynamic memory allocation and Pointers Lecture 4.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
C ++ Basics by Bindra Shrestha sce.uhcl.edu/shresthab CSCI 3333 Data Structures.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Review 1 List Data Structure List operations List Implementation Array Linked List.
C++ (intro) Created by Hwansoo Han Edited by Ikjun Yeom.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
Computer Graphics 3 Lecture 1: Introduction to C/C++ Programming Benjamin Mora 1 University of Wales Swansea Pr. Min Chen Dr. Benjamin Mora.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
11 Introduction to Object Oriented Programming (Continued) Cats.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
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.
C++ Namespaces, Exceptions CSci 588: Data Structures, Algorithms and Software Design All material not from online sources copyright © Travis Desell, 2011.
Pointers, Arrays, And Dynamic Memory Allocation by Bindra Shrestha CSCI 3333 Data Structures.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Pointers and Arrays Dynamic Variables and Arrays.
Object Lifetime and Pointers
Pointers and Dynamic Arrays
Chapter Topics The Basics of a C++ Program Data Types
Basic Elements of C++.
CSC113: Computer Programming (Theory = 03, Lab = 01)
8 Pointers.
Basic Elements of C++ Chapter 2.
Pointers, Dynamic Data, and Reference Types
Created by Hwansoo Han Edited by Ikjun Yeom
DYNAMIC MEMORY MANAGEMENT
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

Pointers by Dr. Bun Yue Professor of Computer Science CSCI 3333 Data Structures

Acknowledgement  Mr. Charles Moen  Dr. Wei Ding  Ms. Krishani Abeysekera

Assumptions  Assume that you are familiar with C, C++ or Java.

Brief Facts About C++  Evolved from C  Designed and implemented by Bjarne Stroustrup at the Bell Labs in the early 1980s  “C with classes”, Better C  Standardized by ISO in 1997 Includes the C++ standard library Standard Template Library (STL)  Part of the C++ standard library  Ready made classes for data structures and algorithms

C++ Filenames  Can be different from the name of the class or the name of the function in the file .cpp Extension for the C++ source code files .h Extension for C++ header files Usually, code for a data structure is put in a header file, and then it can be added to a program with an include directive, e.g. #include "BasicVector.h"  Name of executable file In MSVS it’s the name of your project In g++ it’s either “ a.out ” or a name you specify

A Simple C++ Example /* FILE: main.cpp */ #include using namespace std; int main() { cout << "Enter your first name: "; string name; cin >> name; cout << "Hello, " << name << endl; return 0; //optional }

/* FILE: main.cpp */ #include using namespace std; int main() { cout << "Enter your first name: "; string name; cin >> name; cout << "Hello, " << name << endl; return 0; //optional } A Simple C++ Example Comment C++ header files from C++ standard library “Entry” function Namespace for C++ standard library All C++ statements end with a semicolon

Memory Address  Every byte in the primary memory has an address.  In C++, a pointer is a variable that stores a memory address.  The address (reference) operator & is used to return the address of a variable.

Pointer Type  A pointer variable has a type associated with it, and the memory address it points to should hold data of that type. int myInt = 0; // Allocates memory; stores 0 int *pMyInt; // Declares pointer variable without // initialization

An Example #include using namespace std; int main() { int x = 10; float y = ; string z = "I am a college professor."; bool b = true; cout << "x: value: " << x << " addr: " << &x << endl; cout << "y: value: " << y << " addr: " << &y << endl; cout << "z: value: " << z << " addr: " << &z << endl; cout << "b: value: " << b << " addr: " << &b << endl; }

Output x: value: 10 addr: 0012FF50 y: value: addr: 0012FF44 z: value: I am a college professor. addr: 0012FF1C b: value: 1 addr: 0012FF13

Pointer Type Checking  A pointer variable of a particular type cannot store address of an incompatible type. Error: int x = 10; float* fp_x = &x; MS VS: “Types pointed to are unrelated; conversion requires reinterpret_cast, C- style cast or function-style cast”

Dereferencing  Dereferencing (indirection) operation in C++: *. *p: access the value of the memory address stored in p.  * is heavy overloaded in C++: Number multiplication Pointer variable declaration Dereferencing.

Example … int main() { int x = 10; cout << "value of x = " << x << endl; int* p_x = &x; cout << "value of p_x = " << p_x << endl; int** p_p_x = &p_x; cout << "value of p_p_x = " << p_p_x << endl; int*** p_p_p_x = &p_p_x; cout << "value of p_p_p_x = " << p_p_p_x << endl; *p_x = 15; cout << "value of x = " << x << endl; }

Output of Example value of x = 10 value of p_x = 0012FF60 value of p_p_x = 0012FF54 value of p_p_p_x = 0012FF48 value of x = 15

Example // Dereferencing: cout << "value of *p_x = " << *p_x << endl; cout << "value of *p_p_x = " << *p_p_x << endl; cout << "value of **p_p_x = " << **p_p_x << endl; cout << "value of *p_p_p_x = " << *p_p_p_x << endl; cout << "value of **p_p_p_x = " << **p_p_p_x << endl; cout << "value of ***p_p_p_x = " << ***p_p_p_x << endl;

Output of Example value of *p_x = 10 value of *p_p_x = 0012FF60 value of **p_p_x = 10 value of *p_p_p_x = 0012FF54 value of **p_p_p_x = 0012FF60 value of ***p_p_p_x = 10

Example: Same Effect … int main() { int x = 10; cout << "value of x = " << x << endl; int* p_x = &x; cout << "value of p_x = " << p_x << endl; int** p_p_x = &p_x; cout << "value of p_p_x = " << p_p_x << endl; int*** p_p_p_x = &p_p_x; cout << "value of p_p_p_x = " << p_p_p_x << endl; ***p_p_p_x = 15; // Change from *p_x = 15; cout << "value of x = " << x << endl; }

Where are the errors? int main(){ int m; int *pm; *pm = 5; int n; int *pn = &n; pn = 5; }

Where are the errors? int main(){ int m; int *pm; *pm = 5; int n; int *pn = &n; pn = 5; } ERROR! No address in pm //Correction pm = &m; *pm = 5;

Where are the errors? int main(){ int m; int *pm; *pm = 5; int n; int *pn = &n; pn = 5; } ERROR! No address in pm //Correction pm = &m; *pm = 5; ERROR! Missing operator* //Correction *pn = 5;

Error messages For pn = 5; MS VS: “error C2440: '=' : cannot convert from 'int' to 'int *‘. Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast”

Error messages For int *pm; *pm = 5; MSVS: warning C4101: ‘pm' : unreferenced local variable; warning C4700: uninitialized local variable 'pm' used.

Error messages  Executing the program after comment out pn = 5; (thus no compilation error, just warnings).

Pointer and Array  A name of an array holds the address of the first element in the array.  Thus, an array is just a pointer const (cannot be changed).  This definition is different from the usual definition of array.  Pointers and arrays point to elements of the same type.

Example int a[5] = {1,2,3,4,5}; cout << "a[2] = " << a[2] << endl; cout << "a = " << a << endl; cout << "a+2 = " << (a+2) << endl; cout << "a+4 = " << (a+4) << endl; cout << "a+6 = " << (a+6) << endl; Output: a[2] = 3 a = 0012FF50 a+2 = 0012FF58 a+4 = 0012FF60 a+6 = 0012FF68

Example int a[5] = {1,2,3,4,5}; *(a+3) = 20; cout << " a[3] = " << a[3] << endl; Output: a[3] = 20

Example int a[5] = {1,2,3,4,5}; *(a+5) = 40; cout << "*(a+5) = " << *(a+5) << endl; Output: *(a+5) = 40

Pointer Arithmetic  C/C++ allow pointer arithmetic: + and – for pointer and array, ++ and -- for pointer.  If a and p are array and pointer respectively, the following operations are allowed: p++, p-- a-1, a+20, p+i*j, p-3*j

Example int a[5] = {1,2,3,4,5}; int *p; p = &a[1]; cout << a[0] << ", " << p[-1]<< ", " << *(p - 1) << endl; cout << a[1] << ", " <<p[0]<< ", " << *(p) << endl; cout << a[2] << ", " << p[1]<< ", " << *(p + 1) << endl; Output:  1, 1, 1  2, 2, 2  3, 3, 3

Example int b[5] = {12, 4, 16, 98, 50}; p = b; cout << (*p)++ << endl;// prints 12 cout << *p << endl;// prints 13 cout << *++p << endl;// prints 4 cout << *p << endl; // prints 4 cout << (*p)-- << endl; // prints 4 cout << *p << endl; // prints 3 cout << *--p << endl; // prints 13 cout << *p << endl; // prints 13

Pointer Operations  Operations are adjusted with respect to the location of the element.  Thus, p++ update p to point to the next element, no matter what the type of the element is.

Example double d[5] = {1.0, 3.0, 5.0, 7.0}; cout << "d[2] = " << d[2] << endl; cout << "d = " << (d) << endl; cout << "d+1 = " << (d+1) << endl; cout << "d+2 = " << (d+2) << endl; cout << "d+3 = " << (d+3) << endl; Output: d[2] = 5 d = 0012FEE4 d+1 = 0012FEEC d+2 = 0012FEF4 d+3 = 0012FEFC

Why Pointer Arithmetic  Efficiency!

Example: strcpy char *strcpy (char *dst, const char *src) { int i = 0; for (i = 0; src[i] != '\0'; i++) { dst[i] = src[i]; } dst[i] = '\0'; return dst; }

Example: strcpy using pointers char *strcpy (char *dst, const char *src) { char *retval = dst; while (*dst++ = *src++) ; return retval; }

Pointer Arithmetic  Cryptic (especially using together with ++ and --).  Error-prone.  Avoid using it.

Dynamic Memory Allocation  The proper use of pointer is for dynamic memory allocation.  Data structures of variable sizes can be constructed.

Sizes of data structures  Many data structures require known sizes during their creations.  Examples: arrays int a[5]; double d[MAX]; Student s[20];

Dynamic Sizes  However, many applications require data structures of dynamic sizes.  Examples: Tasks in an operating system Number of bidders in an auction People supporting Obama in Clear Lake

Memory Allocation  The new and new [] operators allocate the necessary memory and return the address.

Example p = new int; *p = 8; cout << "p = " << p << endl; cout << "*p = " << *p << endl; p = new int; // memory leakage. *p = 20; cout << "p = " << p << endl; cout << "*p = " << *p << endl; Output: p = D8 *p = 8 p = *p = 20

Example 2: Where is the error? int main(){ int* p, q; p = new int; *p = 8; q = p; cout << "p = " << p << endl; cout << "q = " << q << endl; p = new int; *p = 20; cout << "p = " << p << endl; cout << "q = " << q << endl; cout << "*p = " << *p << endl; cout << "*q= " << *q << endl; }

Example 2 int main(){ int *p, *q; p = new int; *p = 8; q = p; cout << "p = " << p << endl; cout << "q = " << q << endl; p = new int; *p = 20; cout << "p = " << p << endl; cout << "q = " << q << endl; cout << "*p = " << *p << endl; cout << "*q= " << *q << endl; }

Output of Example 2 p = E0 q = E0 p = q = E0 *p = 20 *q= 8

Memory Allocation  The memory structure for allocation through new operator is usually called heap.  The C++ Runtime may not have enough memory. Example: p = new int[ ]; MSVS: total size of array must not exceed 0x7fffffff bytes

Handling insufficient error int * p = new int [n]; // if it fails an exception bad_alloc is thrown. p = new (nothrow) int [5]; if (p == NULL) { // error allocating memory. };

Memory De-allocation  delete and delete [] are used to de- allocate memory for a single element and an array of elements respectively.  new and delete replaces malloc and free in C.  Example: int * p; … delete p;

Memory Corruption  Unintended pointer operations may cause memory corruption.

Example char *s1; delete s1; cause warning (“uninitialized local variable 's1' used”) and runtime exception.

Example char *s2 = new char[5]; delete s2; cause runtime exception.

Example char *s3; s3[2] = 10; cause runtime exception.

Example char *s4 = new char[10]; s4[10] = 'c'; cout << "s4[10] = " << s4[10] << endl; Output: s4[10] = c No error! But memory is corrupted!

Example char *t1 = new char('a'); char *t2 = t1; cout << "*t2 = " << *t2 << endl; delete t1; cout << "*t2 = " << *t2 << endl; Output: (no error!) *t2 = a *t2 = ▌  No other pointer referring to memory before de- allocation.

Memory Leakage  If the programmer forgets to de- allocate memory allocated by the new operator, memory leakage occurs.  Memory leakage is a notorious problem in C/C++. E.g. Web browsers.

Example char *s = new char; *s = 'c'; cout << "s[0] = " << s[0] << endl; s = new char('d'); cout << "*s = " << *s << endl; *s = 'e'; cout << "*s = " << *s << endl; Output: (with leakage) s[0] = c *s = d *s = e

Delete Before Re-Allocation char *s = new char; *s = 'c'; cout << "s[0] = " << s[0] << endl; delete s; s = new char('d'); cout << "*s = " << *s << endl; *s = 'e'; cout << "*s = " << *s << endl;

Example: Leakage int *i = new int(10); int *j = new int(15); i = j; cout << "*i = " << *i << endl; *j = 20; cout << "*i = " << *i << endl; Output: *i = 15 *i = 20

Example: What is the problem? char *s1 = new char[15]; char *s2 = new char[15]; strcpy(s1, "clinton"); s2 = s1; strcpy(s2, s1); delete [] s1;

Example: No memory corruption char *s1 = new char[15]; char *s2 = new char[15]; strcpy(s1, "clinton"); s2 = s1; strcpy(s2, s1); delete [] s1; delete [] s2;

The Culprit s2 = s1; strcpy(s2, s1);  The two statements have the same effect.  char * is unique as an indirection is automatically done.

C/C++ Dynamic Memory Allocation  Allow custom designed memory management.  Possibly more effective!  Error-prone.  Programming intensive

Java’s Approach  Java has no pointer.  Thus, there is no pointer arithmetic.  Java does have reference.  Programmers usually do not handle memory management.  “delete” is not a keyword in Java.

Automatic Garbage Collection  Java performs automatic garbage collection. Greatly simplify programmer’s work. Much less error-prone. Programmers lose control of memory management. Performance may be an issue, especially for real-time applications.

Java Reference  Java’s variable to objects are reference to the objects. Student s = new Student(“Yue”);  A Student object (Yue) is created.  s refers to the object.

C++ Objects and Pointers  Pointers to objects are similar to other pointers.

Example: C++ Rect r1 (2,3); Rect r2 (10,20); cout << "r1 area: " << r1.area() << endl; cout << "r2 area: " << r2.area() << endl; r1 = r2; cout << "r1 area: " << r1.area() << endl; r2.setWidth(7); cout << "r1 area: " << r1.area() << endl; cout << "r2 area: " << r2.area() << endl; Output: r1 area: 6 r2 area: 200 r1 area: 200 r2 area: 140

Example: Comparing to Java Rect r1 = new Rect(2,3); Rect r1 = new Rect(10,20); r1 = r2; r2.setWidth(7);

Similar Effect in C++ Rect *pr1 = & Rect(2,3); Rect *pr2 = & Rect(10,20); cout area() << endl; cout << "*pr2 area: " << (*pr2).area() << endl; pr1 = pr2; cout << "*pr1 area: " << (*pr1).area() << endl; pr2.setWidth(7); cout << "*pr1 area: " << (*pr1).area() << endl; cout << "*pr2 area: " << (*pr2).area() << endl;

Questions and Comments?