1 Pointers (Walls & Mirrors - Beginning of Chapter 4)

Slides:



Advertisements
Similar presentations
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Advertisements

1 Pointers Lecture Introduction Pointers  Powerful, but difficult to master  Simulate pass-by-reference  Close relationship with arrays and.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
CS 141 Computer Programming 1 1 Pointers. Pointer Variable Declarations and Initialization Pointer variables –Contain memory addresses as values –Normally,
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
@ Zhigang Zhu, CSC212 Data Structure - Section RS Lectures 6/7 Pointers and Dynamic Arrays Instructor: Zhigang Zhu Department of Computer Science.
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.
1 Data Abstraction: The Walls (Walls & Mirrors - Chapter 3)
(Walls & Mirrors - Beginning of Chapter 4)
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
Pointers. COMP104 Pointers / Slide 2 Pointers * A pointer is a variable used for storing the address of a memory cell. * We can use the pointer to reference.
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
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 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.
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
Pointer Data Type and Pointer Variables
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
Pointers CS362. Pointers A Pointer is a variable that can hold a memory address Pointers can be used to: Indirectly reference existing variables (sometimes.
Introduction to Pointers.. What is a pointer? A derived type which holds the address of a variable. Simply a memory location where the variable is stored.
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.
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
Pointers and Dynamic Objects Mechanisms for developing flexible list representations JPC and JWD © 2002 McGraw-Hill, Inc.
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.
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.
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.
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Dynamic Memory. We will follow different order from Course Book We will follow different order from Course Book First we will cover Sect The new.
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.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Review 1 List Data Structure List operations List Implementation Array Linked List.
C++ Programming Lecture 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
POINTERS Introduction to Systems Programming - COMP 1002, 1402.
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.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
UNIT 8 Pointers.
CS162 - Topic #12 Lecture: –Arrays with Structured Elements defining and using arrays of arrays remember pointer arithmetic Programming Project –Any questions?
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Pointers and Arrays Dynamic Variables and Arrays.
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.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
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?
Intro to Pointers in C CSSE 332 Operating Systems
EGR 2261 Unit 11 Pointers and Dynamic Variables
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Pointers Psst… over there.
Andy Wang Object Oriented Programming in C++ COP 3330
Pointers Psst… over there.
Pointer Basics Psst… over there.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
CSC212 Data Structure - Section FG
Data Structures and Algorithms Introduction to Pointers
Pointer Basics Psst… over there.
CISC181 Introduction to Computer Science Dr
Presentation transcript:

1 Pointers (Walls & Mirrors - Beginning of Chapter 4)

2 What’s a Pointer? A pointer variable is a variable that can contain the location of another variable as its value. The location of a variable is usually implemented by indicating its address in (RAM) memory. The location (or address) of a variable is called a pointer. Sometimes, for brevity, a pointer variable is simply called a pointer. You will need to be careful to understand whether pointer refers to a variable or the address of a variable.

3 Pointers -“Real Life” Examples Suppose that your friend, Sam, borrows your copy of Walls & Mirrors. In its place, he leaves you the note Borrowed your Walls & Mirrors book. Thanks, Sam This note is like a pointer, since it it not your book, but it tells you where to go to find it. (The paper on which the note is written is like a pointer variable.)

4 Pointers - Graphical Representation A variable is often represented as a box. The value of the variable is written inside the box. If the variable is a pointer variable, containing a pointer, the box will contain the “tail” of an arrow that points to another variable. Pointer variable Other variable

5 Pointers - Suggestion If you have a problem with pointers, draw the layout. It may be difficult to understand what is going on without a graphical representation of the pointer relationships.

6 Pointer Declarations int *iptr;// iptr is a pointer to an int char *cptr;// cptr is a pointer to a char float *fptr;// fptr is a pointer to a float List *Lptr;// Lptr is a pointer to a List object Sphere *Sptr;// Sptr is a pointer to a Sphere object

7 Pointer Operations Assignment: = –A pointer variable can be assigned only a pointer (i.e. the address of a variable) or NULL (which equals 0). Comparison: = =, != –Pointers can be compared for equality. Addition/Subtraction: +,  –Pointers can be incremented or decremented with an integer. Dereferencing: * –*ptr returns the value of the object pointed to by ptr. Address of: & –&ptr returns the address of ptr (i.e. pointer to ptr).

8 Pointer Operations - Address of The Address of operator & returns the address of an object. float PI = ; float *PIptr; &PI returns the address of the variable PI, not (the value stored in PI). PIptr = &PI stores the address of variable PI in variable, PIptr. &PIptr returns the address of variable PIptr.

9 Pointer Operations - Dereferencing The Dereferencing operator * returns the value of the object to which its operand points. float PI = ; float *PIptr; float X; PIptr = Π// PIptr contains the address of PI X = *PIptr;// Value stored in PI ( ) is // assigned to X *(*(&PIptr)) = *PIptr = *(&PI) = PI =

10 Pointer Initialization int *ptr;// pointer to int declared, value undefined int x = 5;// int declared and initialized to 5 cout << x;// prints 5 cout << *ptr;// Error! Prints undefined value, since ptr not // initialized ptr = &x;// ptr now contains the address of x cout << *ptr;// prints 5

11 Pointer Initialization - Suggestion When a pointer variable is declared it is (by default) uninitialized. Therefore, where it is pointing is undefined. It’s a good practice to initialize newly declared pointer variables to the NULL pointer (= 0). –This will insure that the pointer variable is not pointing anywhere it shouldn’t. –This will help you determine if a valid pointer has been assigned to it. if( ptr = = NULL ) cout << “ptr has not been initialized” << endl;

12 new Operator The operator new creates a new object of a given type. new returns a pointer to the newly created object. ptr = new int; ptr new int variable

13 new Operator (Cont’d.) An object created with new does not have a name and is not declared. An object created with new can only be used by following (dereferencing) a pointer to it. You need to be careful to not lose the pointer to an object created with new, since there is no other way to access it. Memory that was allocated with new and has become inaccessible is called a memory leak. For programs that run for long periods of time, memory leaks can be the reason for system failure.

14 new Operator - Example 1 int *ptr;// pointer to int declared, value undefined *ptr = 5;// Error! ptr contains invalid address and // space for int not allocated ptr = new int;// space for int allocated and pointer to it // assigned to ptr *ptr = 5;// 5 is stored in the int pointed to by ptr

15 new Operator - Example 2 int *p, *q;// declare two pointer to int variables p = new int;// allocate space for an int; make p point to it *p = 25;// store 25 in the int pointed to by p What is the effect of the following? q = p;

16 new Operator - Example 2 (Cont’d.) Draw a picture! qp new int 25

17 new Operator - Example 3 int *p, *q;// declare two pointer to int variables p = new int;// allocate space for an int; make p point to it q = new int;// allocate space for an int; make q point to it *p = 35;// store 35 in the int pointed to by p What is the effect of the following? *q = *p;

18 new Operator - Example 3 (Cont’d.) Draw a picture! p new int35 q new int35

19 new Operator - Example 3 (Cont’d.) What would have happened if we had executed q = p; instead of *q = *p;

20 new Operator - Example 3 (Cont’d.) The new int, previously pointed to by q is LOST and cannot be recovered. This is called a memory leak. p new int35new int? q

21 Arrays and Pointers int a[50]; int *aptr = a; ais equivalent to&a[0] aptr = a;is equivalent toaptr = &a[0]; aptr+5is equivalent to&a[5] *(aptr+5)is equivalent toa[5]