Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.

Slides:



Advertisements
Similar presentations
1 Pointers. Variable Memory Snapshot 2 int nrate = 10; The variable is stored at specific memory address A variable is nothing more than a convenient.
Advertisements

Engineering Problem Solving with C Fundamental Concepts Chapter 6 Pointers.
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.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 10: Pointers by.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
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.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 9: Pointers.
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.
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.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified by use by the MSU CMPS Dept. Chapter 10:
C++ Pointers Copies from SEE C++ programming course and from Starting Out with C++: Early Objects, 8/E by Tony Gaddis, Judy Walters and Godfrey Muganda.
Chapter 6 Pointers C Programming © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Chapter 9 Pointers Fall 2005 Csc 125 Introduction to C++
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.
1 Programming with Pointers Turgay Korkmaz Office: SB Phone: (210) Fax: (210) web:
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9: Pointers.
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.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
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.
Dynamic memory allocation and Pointers Lecture 4.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
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.
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.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 22: Pointers.
C++ Programming Lecture 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Pointers *, &, array similarities, functions, sizeof.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
POINTERS Introduction to Systems Programming - COMP 1002, 1402.
DCT1063 Programming 2 CHAPTER 1 POINTERS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
1  Lecture 12 – Pointer FTMK, UTeM – Sem /2014.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Pointers. Introduction to pointers Pointer variables contain memory addresses as their values. Usually, a variable directly contains a specific value.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
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.
Pointers CSC1201: Programming Language 2. Topics Pointers ▫Memory addresses ▫Declaration ▫Dereferencing a pointer ▫Pointers to pointer.
Pointers What is the data type of pointer variables?
Chapter 9: Pointers.
Lecture 6 C++ Programming
Andy Wang Object Oriented Programming in C++ COP 3330
Dynamic Memory Allocation
An Introduction to Pointers
Object Oriented Programming COP3330 / CGS5409
Chapter 9: Pointers.
Pointers, Dynamic Data, and Reference Types
Programming with Pointers
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Standard Version of Starting Out with C++, 4th Edition
DYNAMIC MEMORY MANAGEMENT
Pointers, Dynamic Data, and Reference Types
Pointers.
Presentation transcript:

Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures

Pointer Basics A pointer is a variable that holds the memory address of another object If a variable p contains the address of another variable q, then p is said to point to q If q is a variable at location 0x100 in memory, then p would have the value 0x100 (q’s address)

Address Of Operator The & operator is called the address of operator. The operator & in front of any variable produces the address of that variable. Example: int x=75; cout << "x is " << x; cout << "\nthe addres of x is " << &x; 75 x 0x7fff8164 x is 75 the address of x is 0x7fff8164

How to declare a pointer variable pointer variables are declared using the indirection operator *, more commonly called the dereferencing operator. general form - –type *variable_name, *variable_name; –type* variable_name; when declaring more than one pointer variable, the * must precede each variable.

Example int *iPtr; double *dPtr; the variable iPtr is declared to be of type pointer to int the variable dPtr is declared to be of type pointer to double neither variable in this example has been initialized declaring a pointer creates a variable capable of holding an address

Example int *iPtr, i=6; char *s, str = "example"; double *dPtr, d=1.25; iPtr s dPtr 6 "example" 1.25 i str d

NULL pointer there is a literal value that can be assigned to any pointer the literal value is NULL or 0 in this context it is known as the null address and does not point to anything which can be referenced. trying to dereference the null address, results in a fatal error

Example int *iPtr=0; char *s=NULL; //predefined constant in iostream.h double *dPtr=NULL; NULL iPtr s dPtr

Assigning values to a pointer the assignment operator (=) is defined for pointers the right operand of the assignment operator can be any expression that evaluates to the same type as the left Example: int x, *xp, *ip; xp = &x; ip = xp; x xp ip

The Base Type In the declaration int *p;// the base type of p is int double *q;// the base type of q is double Base type determines the size of an object to which the pointer is assumed to be pointing The size of p and q are the same ( 4 bytes**), but p points to 4 bytes** and q points to 8 bytes** ** compiler dependent

Base Type continued A pointer’s base type determines how the contents of the memory location pointed to by the pointer will be interpreted The declaration: int *p; declares p to be a pointer to int. Whatever p points to will be interpreted as an int Base type also affects pointer arithmetic

Assigning/Using Values To assign a value using a pointer, use the * to dereference the pointer: int *iptr, intVal; iptr = &intVal; // assign iptr to addr intVal *iptr = 5; // make contents = 5 cout << *iptr << endl; // display contents cout << iptr << endl; // display address

Comparing Pointers You may compare pointers using relational operators Common comparisons are: –check for null pointer (p == NULL) –Note: since NULL evaluates as false, and any other pointer evaluates as true, checking for a null pointer can be done as (!p) –check if two pointers are pointing to the same object (p == q) –Note: (*p == *q) means they are pointing to equivalent, but not necessarily the same data.

Pointers and Arrays The name of an array is the address of the first element (i.e. a pointer to the first element) Arrays and pointers may often be used interchangeably Example int num[4] = {1,2,3,4}, *p; p = num;//same as p = &num[0]; cout << *p<<endl; p++; cout << *p; 1212

More Pointers and Arrays You can also index a pointer using array notation Example: char myString[] = "This is a string"; char *str; str = myString; for(int i =0; str[i]; i++)//look for null cout << str[i]; What does this segment print? This is a string

Arrays and Pointers The name of an array is a pointer to the first element. However, the array name is not a pointer variable. It is a constant pointer that always points to the first element of the array and its value can not be changed. Also when an array is declared, space for all elements in the array is allocated, however when a pointer variable is declared only space sufficient to hold an address is allocated

Common Pointer Problems Using uninitialized pointers int *iPtr; *iPtr = 100; iPtr has not been initialized. The value 100 will be assigned to some memory location. Which one determines the error. Failing to reset a pointer after altering it’s value Incorrect/unintended syntax

Dynamic Allocation Using new and delete Storage for data is allocated as needed from the free memory area known as the heap (or the freestore). Run-time stack –Local variables –Formal parameters –Managed by the compiler Heap –Dynamic storage –Managed by storage allocator Stack Heap Global data Program code

Dynamic Memory Allocation Dynamically allocated memory is determined at runtime It allows a program to create as many or as few variables as required, offering greater flexibility Dynamic allocation is often used to support data structures such as stacks, queues, linked lists and binary trees. Dynamic memory is finite Dynamically allocated memory may be freed during execution

Operator new Returns the address of memory allocated on the heap If allocation fails, new terminates the program (or returns NULL on older compilers)

Examples int *iPtr; iPtr = new int; // iPtr points to new // integer variable on heap double *dPtr; dPtr = new double[20];// dPtr points to the first // double in an array // of 20 doubles on heap

int main() { int *iPtr, i; iPtr = new int; *iPtr = 7; double *dPtr; dPtr = new double[6]; cout << *iPtr << endl; for(i=0; i<6; i++) //SKETCH MEMORY dPtr[i] = 5; cout << endl; for(i=0; i<6; i++) cout << dPtr[i] << ' '; return 0; } OUTPUT iPtr dptr Practice: 7

Operator delete The delete operator frees memory produced by new Using delete to attempt to free any other type of address will produce problems While delete uses a pointer variable, it does not destroy the pointer variable, it only frees the storage to which the variable is pointing.

delete Example int *ptr; ptr = new int (100); // can initialize, just like int cout << *ptr;//see if it worked delete ptr;//free the memory value of ptr is now undefined 100

Example of dynamically allocated arrays double *dptr; const int size = 10; dptr = new double[size];//allocate 10 doubles for(int I=0; I<size; I++)//read values into array cin >> dptr[I]; fun1(dptr, size);// pass array to fun1 delete [] dptr;//free all 10 elements prototype for fun1: void fun1(double*, int);

Example using delete int *iPtr, *jPtr; iPtr = new int[3]; for (int i=0; i<3; ++i) iPtr[i] = i; for (int i=0; i<3; ++i) cout << iPtr[i] << endl; delete [] iPtr; jPtr = new int; *jPtr = 5; cout << *jPtr * 7 << endl; delete jPtr;