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.

Slides:



Advertisements
Similar presentations
Chapter 6 Data Types
Advertisements

Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
A pointer is the memory address of a variable. A memory address is a physical location within a system’s memory space. A pointer variable is variable used.
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
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.
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
Pointers and dynamic objects COMP171 Fall Pointers and dynamic objects/ Slide 2 Topics * Pointers n Memory addresses n Declaration n Dereferencing.
Pointer. Warning! Dangerous Curves C (and C++) have just about the most powerful, flexible and dangerous pointers in the world. –Most other languages.
Pointers CS 308 – Data Structures. Getting the address of a variable You need to use the address operator & #include void main() { int num; num = 22;
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.
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.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Memory and C++ Pointers.  C++ objects and memory  C++ primitive types and memory  Note: “primitive types” = int, long, float, double, char, … January.
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
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.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Pointers CSE 5100 Data Structures and Algorithms.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
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.
Semantics of Arrays and Pointers By: M. Reza Heydarian Introduction Pointers Arrays Semantics of Arrays Semantics of Pointers.
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.
SPL – Practical Session 2 Topics: – C++ Memory Management – Pointers.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Chapter 9 Pointers and Dynamic Arrays (9.1). Pointers A variables which holds the memory address for a variable of a specific type. Call-by-Reference.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Object-Oriented Programming in C++
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Dynamic Allocation Joe Meehean. Dynamic Allocation Memory for local objects is automatically created and reclaimed memory is created for it at beginning.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
C HAPTER 03 Pointers Compiled by Dr. Mohammad Alhawarat.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Lecture 10: 2/17/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
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.
P OINTERS A pointer is an address All data is stored in memory in some location that is indexed with an address Can refer to variables by name or by memory.
Pointer Data Type and Pointer Variables III CS1201: Programming Language 2 By: Nouf Aljaffan Edited by : Nouf Almunyif.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
POINTERS AND MEMORY ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
Dynamic Allocation in C
Dynamic Memory CSCE 121 J. Michael Moore.
Dynamic Memory Allocation
Pointer Basics Psst… over there.
Pointers, Dynamic Data, and Reference Types
Pointers And Memory Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
Chapter 12 Pointers and Memory Management
Dynamic Memory.
Pointer Basics Psst… over there.
Dynamic Memory CSCE 121.
Pointers, Dynamic Data, and Reference Types
Run-time environments
SPL – PS2 C++ Memory Handling.
Presentation transcript:

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 a memory cell initially undefined a "pointer to int" declaration int * p; // pointer variable p itself is static here Typically, pointer variables are used to store memory address of dynamically allocated objects (e.g. int for p to point at) p = new int;

7a-3 Pointers To help keep things straight, it’s useful to pretend there are parentheses in the pointer declarations: (int *) p; means that p is of type int* (i.e.: that is, p is a pointer to an int location) Don’t actually write declarations this way

7a-4 Dereferencing Operator The expression *p denotes the memory cell to which p points Here, * is called the dereferencing operator Be careful not to dereference a pointer that has not yet been initialized: int * p; p ? *p = 7; Address in p could be any memory location Attempt to put a value into an unknown memory location will result in a run-time error, or worse, a logic error

7a-5 The Null Pointer The null pointer is a special constant which is used to explicitly indicate that a pointer does not point anywhere NULL is defined in the standard library (or ) In diagrams, indicated as one of: NULL.

7a-6 The Address Operator The expression &x denotes the address of a variable x Here, & is called the address operator int x, *p; p ? x ? p = &x; *p = 4; p x ? p x 4 Value of x has been changed by *p = 4;

7a-7 Pointer Example int *p, *q, x; p = &x; *p = 4; p x (or *p) q 4 ? p = new int; p x q 4 ? Call stackHeap ? *p

7a-8 Pointer Example (cont’d) *p = 8; p x q 4 ? 8 q = p; Call stackHeap p x q 4 8 *p *p or *q

7a-9 Pointer Example (cont’d) q = new int; *q = 5; Call stackHeap p x q 4 8 *p 5 *q

7a-10 Pointer Example (cont’d) p = NULL; Call stackHeap p x q *q. This location is no longer has a name, is no longer reachable from the program, and cannot be deallocated: we have a memory leak

7a-11 Pointer Example (cont’d) delete q; q = NULL; Call stackHeap p x q Allocated but unreachable. Deallocated, and available for reuse

7a-12 Dynamic Allocation of Arrays Use the new operator to allocate an array dynamically An array name is really a pointer to the array’s first element double* arr; arr = new double[5]; Call stackHeap arr ? ? ? ? ?

7a-13 Arrays of Pointers It’s possible to have arrays of pointers The array name is a pointer to an array of pointers: int** arrayOfPtr; int j = 6; k = 4; arrayOfPtr = new int*[4]; jk 64 arrayOfPtr ???? 0123 Allocated in heap space; pointers in array are not initialized yet

7a-14 Arrays of Pointers arrayOfPtr[0] = &k; arrayOfPtr[2]=&j; arrayOfPtr[3] = new int; j k 6 4 arrayOfPtr Call stackHeap ? 0123 ?