Pointers and dynamic objects

Slides:



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

This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic 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.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Pointers and dynamic objects COMP171 Fall Pointers and dynamic objects/ Slide 2 Topics * Pointers n Memory addresses n Declaration n Dereferencing.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
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.
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.
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
Pointers. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program execution.
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.
Chapter 9 Pointers Fall 2005 Csc 125 Introduction to C++
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
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.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Review 1 List Data Structure List operations List Implementation Array Linked List.
Lecture 7 Pointers & Refrence 1. Background 1.1 Variables and Memory  When you declare a variable, the computer associates the variable name with a particular.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
Variables and memory addresses
1 2/2/05CS250 Introduction to Computer Science II Pointers.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
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 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.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
Pointers What is the data type of pointer variables?
Intro to Pointers in C CSSE 332 Operating Systems
Overview 4 major memory segments Key differences from Java stack
Motivation and Overview
Command Line Arguments
Pointers and Pointer-Based Strings
CSC113: Computer Programming (Theory = 03, Lab = 01)
Lecture 6 C++ Programming
Pointers Psst… over there.
void Pointers Lesson xx
Pointers and References
Dynamic Memory Allocation Reference Variables
Pointers Psst… over there.
CSC 253 Lecture 8.
Object Oriented Programming COP3330 / CGS5409
CSC 253 Lecture 8.
Overview 4 major memory segments Key differences from Java stack
Pointers and dynamic objects
Popping Items Off a Stack Lesson xx
Pointers, Dynamic Data, and Reference Types
Pointers & Functions.
Chapter 12 Pointers and Memory Management
Computer Skills2 for Scientific Colleges
Pointers Kingdom of Saudi Arabia
Pass by Reference.
Arrays Topics to cover: Arrays Data Types One-dimensional Arrays
Pointers.
Overloading functions
C++ Programming Lecture 17 Pointers – Part I
Pointers and Pointer-Based Strings
Data Structures and Algorithms Introduction to Pointers
Pointers & Functions.
Standard Version of Starting Out with C++, 4th Edition
CS 144 Advanced C++ Programming February 12 Class Meeting
Functions Reasons Concepts Passing arguments to a function
Pointers and dynamic objects
Pointers and References
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

Pointers and dynamic objects COMP171 Fall 2006 Pointers and dynamic objects

Topics Pointers Static vs. dynamic objects Memory addresses Declaration Dereferencing a pointer Pointers to pointer Static vs. dynamic objects new and delete

Computer Memory Each variable is assigned a memory slot (the size depends on the data type) and the variable’s data is stored there Memory address: 1020 1024 1032 … … 100 … 1024 … a Variable a’s value, i.e., 100, is stored at memory location 1024 int a = 100;

Pointers A pointer is a variable used to store the address of a memory cell. We can use the pointer to reference this memory cell Memory address: 1020 1024 1032 … … 100 … 1024 … integer pointer

Pointer Types Pointer C++ has pointer types for each type of object Pointers to int objects Pointers to char objects Pointers to user-defined objects (e.g., RationalNumber) Even pointers to pointers Pointers to pointers to int objects

Pointer Variable Declaration of Pointer variables type* pointer_name; //or type *pointer_name; where type is the type of data pointed to (e.g. int, char, double) Examples: int *n; RationalNumber *r; int **p; // pointer to pointer

… … … … Address Operator & … The "address of " operator (&) gives the memory address of the variable Usage: &variable_name Memory address: 1020 1024 … … 100 … … … a int a = 100; //get the value, cout << a; //prints 100 //get the memory address cout << &a; //prints 1024

… Address Operator & 100 88 Memory address: 1024 1032 1020 b #include <iostream> using namespace std; void main(){ int a, b; a = 88; b = 100; cout << "The address of a is: " << &a << endl; cout << "The address of b is: " << &b << endl; } Result is: The address of a is: 1020 The address of b is: 1024

Pointer Variables The value of pointer p is the address of variable a A pointer is also a variable, so it has its own memory address 100 88 … 1024 Memory address: 1032 1020 a p int a = 100; int *p = &a; cout << a << " " << &a <<endl; cout << p << " " << &p <<endl; Result is: 100 1024 1024 1032

Pointer to Pointer What is the output? 58 58 58

Dereferencing Operator * We can access to the value stored in the variable pointed to by using the dereferencing operator (*), Memory address: 1020 1024 1032 … 88 100 … 1024 … a p int a = 100; int *p = &a; cout << a << endl; cout << &a << endl; cout << p << " " << *p << endl; cout << &p << endl; Result is: 100 1024 1024 100 1032

Don’t get confused Declaring a pointer means only that it is a pointer: int *p; Don’t be confused with the dereferencing operator, which is also written with an asterisk (*). They are simply two different tasks represented with the same sign int a = 100, b = 88, c = 8; int *p1 = &a, *p2, *p3 = &c; p2 = &b; // p2 points to b p2 = p1; // p2 points to a b = *p3; //assign c to b *p2 = *p3; //assign c to a cout << a << b << c; Result is: 888

A Pointer Example Memory Layout a gets 18 Box diagram main 16 8192 a The code void doubleIt(int x, int * p) { *p = 2 * x; } int main(int argc, const char * argv[]) int a = 16; doubleIt(9, &a); return 0; Box diagram main 16 p (8200) 8192 a doubleIt x (8196) 9 a (8192) 16 main Note the signature of main() – it’s an array parameter. And, like other examples we’ve given, it takes the number of elements as a parameter. Why? doubleIt 9 x a gets 18 p

Another Pointer Example Let’s figure out: value1==? / value2==? Also, p1=? p2=? #include <iostream> using namespace std; int main (){ int value1 = 5, value2 = 15; int *p1, *p2; p1 = &value1; // p1 = address of value1 p2 = &value2; // p2 = address of value2 *p1 = 10; // value pointed to by p1=10 *p2 = *p1; // value pointed to by p2= value // pointed to by p1 p1 = p2; // p1 = p2 (pointer value copied) *p1 = 20; // value pointed to by p1 = 20 cout << "value1==" << value1 << "/ value2==" << value2; return 0; }

Reference Variables A reference is an additional name to an existing memory location 9 x ref Pointer: 9 x ref Reference: References fix some of those pointer problems. If we wanted something called “ref” to point to a variable x, we’d declare a pointer variable and assign the address of x into it. With references, we’d attach an additional name – ref – to the same memory location as x. Note how the pointer necessitates an extra variable, whereas the reference didn’t int x = 9; int &ref = x; int x=9; int *ref; ref = &x;

Reference Variables A reference variable serves as an alternative name for an object int m = 10; int &j = m; // j is a reference variable cout << “value of m = “ << m << endl; //print 10 j = 18; // print 18

Reference Variables A reference variable always refers to the same object. Assigning a reference variable with a new value actually changes the value of the referred object. Reference variables are commonly used for parameter passing to a function

Traditional Pointer Usage void IndirectSwap(char *Ptr1, char *Ptr2){ char temp = *Ptr1; *Ptr1 = *Ptr2; *Ptr2 = temp; } int main() { char a = 'y'; char b = 'n'; IndirectSwap(&a, &b); cout << a << b << endl; return 0;

Pass by Reference void IndirectSwap(char& y, char& z) { } int main() { char temp = y; y = z; z = temp; } int main() { char a = 'y'; char b = 'n'; IndirectSwap(a, b); cout << a << b << endl; return 0;

Pointers and Arrays The name of an array points only to the first element not the whole array. 1000 1004 1008 1012 1016

Array Name is a pointer constant #include <iostream> using namespace std; void main (){ int a[5]; cout << "Address of a[0]: " << &a[0] << endl << "Name as pointer: " << a << endl; } Result: Address of a[0]: 0x0065FDE4 Name as pointer: 0x0065FDE4

Dereferencing An Array Name This element is called a[0] or *a #include <iostream> using namespace std; void main(){ int a[5] = {2,4,6,8,22}; cout << *a << " " << a[0]; } //main a[0] 2 a a[1] 4 a[2] 6 a[3] 8 a[4] 22 a

Array Names as Pointers To access an array, any pointer to the first element can be used instead of the name of the array. We could replace *p by *a #include <iostream> using namespace std; void main(){ int a[5] = {2,4,6,8,22}; int *p = a; cout << a[0] << " " << *p; } 2 4 8 6 22 a[4] a[0] a[2] a[1] a[3] a p 2 2

Multiple Array Pointers Both a and p are pointers to the same array. #include <iostream> using namespace std; void main(){ int a[5] = {2,4,6,8,22}; int *p = &a[1]; cout << a[0] << " " << p[-1]; cout << a[1] << " " << p[0]; } a[0] 2 2 4 4 a[0] 2 p a[1] 4 a[2] 6 p[0] a[3] 8 a[4] 22

Pointer Arithmetic Given a pointer p, p+n refers to the element that is offset from p by n positions. 2 4 8 6 22 p - 1 a p a + 1 p + 1 a + 2 p + 2 a + 3 a + 4 p + 3

Dereferencing Array Pointers a[3] or *(a + 3) a[2] or *(a + 2) a[1] or *(a + 1) a[0] or *(a + 0) a[4] or *(a + 4) 2 4 8 6 22 a a + 2 a + 4 a + 3 a + 1 *(a+n) is identical to a[n] Note: flexible pointer syntax

Array of Pointers & Pointers to Array b c A pointer to an array An array of Pointers int a = 1, b = 2, c = 3; int *p[5]; p[0] = &a; p[1] = &b; p[2] = &c; int list[5] = {9, 8, 7, 6, 5}; int *p; P = list;//points to 1st entry P = &list[0];//points to 1st entry P = &list[1];//points to 2nd entry P = list + 1; //points to 2nd entry