Andy Wang Object Oriented Programming in C++ COP 3330

Slides:



Advertisements
Similar presentations
Data Structures (Second Part) Lecture 2 : Pointers Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Advertisements

This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Informática II Prof. Dr. Gustavo Patiño MJ
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
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.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
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: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
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 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
Chapter 6 Pointers C Programming © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
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.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
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.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Dynamic memory allocation and Pointers Lecture 4.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
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.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
Pointers Class #9 – Pointers Pointers Pointers are among C++ ’ s most powerful, yet most difficult concepts to master. We ’ ve seen how we can use references.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
System Programming Practical Session 7 C++ Memory Handling.
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.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
Variables and Types. Primitive Built-in Type Type Meaning Minimum Size bool boolean NA char character 8 bits wchar_t wide character 16 bits char16_t Unicode.
Pointers. What Is Pointer l every variable has memory address char c=’y’; int i=2; address of variable i is 0022 l address can used to refer to this variable.
Basic Concepts:- Invalid use of Address Operator &75 &(‘a’) &(a+b)
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Pointers What is the data type of pointer variables?
Chapter 7 Pointers and C-Strings
Chapter 9: Pointers.
Pointers.
Object Oriented Programming COP3330 / CGS5409
Pointers and Pointer-Based Strings
Lecture 6 C++ Programming
Pointers Psst… over there.
Pointers and References
Pointer Data Type and Pointer Variables
Pointers Psst… over there.
Dynamic Memory Allocation
Pointer Basics Psst… over there.
Chapter 2 Elementary Programming
Object Oriented Programming COP3330 / CGS5409
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
Pointers & Functions.
Pointers Lecture 1 Thu, Jan 15, 2004.
C++ Pointers and Strings
Variables & Basic Types
Pointers and Pointer-Based Strings
POINTER CONCEPT 4/15/2019.
Pointers and dynamic objects
Pointers & Functions.
Pointer Data Type and Pointer Variables
COP 3330 Object-oriented Programming in C++
Pointer Basics Psst… over there.
Pointer Data Type and Pointer Variables
C++ Pointers and Strings
Pointers, Dynamic Data, and Reference Types
POINTER CONCEPT 8/3/2019.
Presentation transcript:

Andy Wang Object Oriented Programming in C++ COP 3330 Pointer Basics Andy Wang Object Oriented Programming in C++ COP 3330

What is a Pointer? A variable that stores a memory address To declare a pointer use the * operator with the following format typeName *variableName; int n; int *p; // pointer to an int All pointers (memory addresses) have the same size. Thus, the type information is important to distinguish them from one another double *dptr; // pointer to a double char *cpt1; // pointer to a char float *fptr; // pointer to a flot

Note These three declarations are equivalent int *p; int * p; int* p; Tricky when declaring multiple variables int x, y, z; // three variables of type int int* p, q, r; // one pointer to int and two integers int *p, *q, *r; // three pointers to int

Pointer Dereferencing To access the target pointed by the pointer, we need to dereference the pointer with the unary * operator int *ptr; // a pointer to int ptr is a variable holding a memory address of an int *ptr dereferences the pointer, which points to the int Note When declaring a pointer, you need to use the * operator AFTER that, you use * only to dereferencing the pointer A pointer may not point to a valid target A pointer may not be initialized Can get runtime error message “segmentation fault”

Pointer Example http://www.cs.fsu.edu/~myers/c++/examples/pointers /pdeclare.cpp

Pointer Example #include <iostream> using namespace std; int main() { int *ptr1, *ptr2; double *dptr; cout << “The value of ptr1 = “ << ptr1 << endl; cout << “The value of ptr2 = “ << ptr2 << endl; cout << “The value of dptr = “ << dptr << endl; // DANGEROUS! Deferecing uninitialized pointers cout << “*ptr1 = “ << *ptr1 << endl; cout << “*ptr2 = “ << *ptr2 << endl; cout << “*dptr = “ << *dptr << endl; }

Initializing Pointers What can we assign to pointers at initialization time? Null Pointers of the same type Address of an variable of the same type

Null Pointer 0 is also known as the null pointer The only integer literal that may be assigned to a pointer int *p = 0; // okay int *q; q = 0; // okay int *z; z = 900; // BAD! A null pointer is never a valid target If you deference a null pointer, you will get a segmentation fault We use null instead of random memory addresses, so we know currently the pointer is not pointing to a valid target To check, do the following if (ptr != 0) cout << *ptr;

Null Pointer Example http://www.cs.fsu.edu/~myers/c++/examples/pointers /pnull.cpp

Null Pointer Example #include <iostream> using namespace std; int main() { int *ptr; cout << “The value of ptr = “ << ptr << endl; cout << “Now initializing ptr to null pointer” << endl; ptr = 0; if (ptr == 0) cout << “Pointer unsafe to dereference” << endl; else cout << “Pointer is safe to dereference” << endl; cout << “Attempting to dereference ptr” << endl; cout << “*ptr = “ << *ptr << endl; }

Pointers of the Same Type Legal to assign pointers of the same type int *ptr1, *ptr2; ptr1 = ptr2 // okay Although all pointers are addresses, different types of pointers are treated differently Automatic type coercisons do not apply int *ip; char *cp; ip = cp; // ILLEGAL To force a coercion, perform an explicit cast ip = reinterpret_castint<int *>(cp) // better know what you // are doing

The “Address of” Operator Recall the unary operator &, applied to a variable, gives its address int *ptr; int num; ptr = &num; // assign the address of num to ptr

Address of Operator Example http://www.cs.fsu.edu/~myers/c++/examples/pointers /pinit.cpp

Address of Operator Example #include <iostream> using namespace std; int main() { int *ip1, *ip2; double *dp1, *dp2; char *cp1, *cp2; int x = 5; double a = 1.1; char ch = ‘$’; ip2 = &x; dp2 = &a; cp2 = &ch;

Address of Operator Example cout << “ip2 = “ << ip2 << “\t &x = “ << &x << endl; cout << “dp2 = “ << dp2 << “\t &a = “ << &a << endl; cout << “cp2 = “ << cp2 << “\t &ch = “ << &ch << endl; cout << “cp2 = “ << reinterpret_cast<void *>(cp2) << “\t &ch = “ << reinterpret_cast<void *>(&ch) << endl << endl; cout << “*ip2 = “ << *ip2 << “\t x = “ << x << endl; cout << “*dp2 = “ << *dp2 << “\t a = “ << a << endl; cout << “*cp2 = “ << *cp2 << “\t ch = “ << ch << “\n\n”; ip1 = ip2; dp1 = dp2; cp1 = cp2; Would try to print a null terminated char string

Address of Operator Example cout << “ip1 points to “ << *ip1 << endl; cout << “dp1 points to “ << *dp1 << endl; cout << “cp1 points to “ << *cp1 << endl; // ATTEMPTING ILLEGAL OPERATIONS /* ip1 = cp1; dp1 = ip1; cp1 = &a; */

Address of Operator Example dp1 = reinterpret_cast<double *>(ip2); ip1 = reinterpret_cast<int *>(dp2); cout << “ip1 points to “ << *ip1 << endl; cout << “dp1 points to “ << *dp1 << endl; cout << “\nip2 = “ << ip2 << endl; cout << “&*ip2 = “ << &*ip2 << endl << endl; cout << “x = “ << x << endl; cout << “*&x = “ << *&x << endl; return 0; }

Array Variables and Pointer Variables Array variable is just a kind of a pointer variable, pointing to the first indexed variable of the array int a[10]; int *p, *p2 = 0; Since both are pointers, it is okay to do the following p = a; // p points to the starting location of a[10] Thus p[0] is the same as a[0] However, the following is illegal a = p2; // ILLEGAL The type of a is const version of int *, which cannot be changed