Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.

Slides:



Advertisements
Similar presentations
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Advertisements

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.
Chapter 9. 2 Objectives You should be able to describe: Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming.
Dynamic Objects. COMP104 Dynamic Objects / Slide 2 Memory Management * Static Memory Allocation n Memory is allocated at compiling time * Dynamic Memory.
Dynamic Objects. COMP104 Dynamic Objects / Slide 2 Memory Management * Static Memory Allocation n Memory is allocated at compilation time * Dynamic Memory.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Pointers and Dynamic Objects Mechanisms for developing flexible list representations.
Pointers and dynamic objects COMP171 Fall Pointers and dynamic objects/ Slide 2 Topics * Pointers n Memory addresses n Declaration n Dereferencing.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Copyright 2004 Scott/Jones Publishing Starting Out with C++: Early.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
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.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
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.
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.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
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.
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 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.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
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.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Pointers and Dynamic Objects Mechanisms for developing flexible list representations JPC and JWD © 2002 McGraw-Hill, Inc.
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.
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.
Object-Oriented Programming in C++
Dynamic memory allocation and Pointers Lecture 4.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
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.
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 
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-1 Pointer Variables Pointer variable : Often just called a pointer, it's.
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.
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
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?
Pointers and Pointer-Based Strings
Pointers Revisited What is variable address, name, value?
CSCE 210 Data Structures and Algorithms
Pointers Psst… over there.
Pointers Psst… over there.
Dynamic Memory Allocation
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Pointers and dynamic objects
Pointers, Dynamic Data, and Reference Types
Pointers.
C++ Pointers and Strings
Dynamic Objects.
Pointers and Pointer-Based Strings
Pointers and dynamic objects
Standard Version of Starting Out with C++, 4th Edition
Pointers and dynamic objects
C++ Pointers and Strings
Pointers, Dynamic Data, and Reference Types
Dynamic Objects.
Presentation transcript:

Review on pointers and dynamic objects

Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory is allocated at running time { int a[200]; … } { int n; cin >> n; a[n]??? }

Static vs. Dynamic Objects  Static object  Memory is acquired automatically  Memory is returned automatically when object goes out of scope  Dynamic object  Memory is acquired by program with an allocation request  new operation  Dynamic objects can exist beyond the function in which they were allocated  Object memory is returned by a deallocation request  delete operation

Why pointers? Dynamic objects are ‘implemented’ or ‘realized’ by ‘pointers’ which are parts of low-level ‘physical memory’ We don’t like it, but can not avoid it. Low level languages directly manipulate them High level languages want to ‘hide’ the pointers (conceptually remove them)

Pointers  A pointer is a variable used for storing the address of a memory cell.  We can use the pointer to reference this memory cell 100 ……1024… Memory address: … 1020 Integer a Pointer p int a; int* p; a

Getting an address: address operator & int a=100; “&a”  “the address of a” 100………… Memory address:1024 int a = 100; cout << a;  100 Cout << &a;  1024 … 1020 a

Dereferencing Operator *  We can access to the value stored in the variable pointed to by preceding the pointer with the “star” operator (*), 10088…1024… Memory address: … 1020 int a = 100; int* p = &a; cout << a << endl; cout << &a << endl; cout << p << " " << *p << endl; cout << &p << endl; a p *p gives 100

Pointer to pointer … int a; int* p; int** q; a = 58; p = &a; q = &p; a pq 58 a, *p, and **q are the same object whose value is 58! But q = &a is illegal!

An asterisk (‘*’) has two usages  In a definition, an asterisk indicates that the object is a pointer. char* s; // s is of type pointer to char (char *s; is possible)  In expressions, an asterisk before a pointer indicates the object the pointer pointed to, called dereferencing int i = 1, j; int* ptr; // ptr is an int pointer ptr = &i; // ptr points to i j = *ptr + 1;// j is assigned 2 cout << *ptr << j << endl; // display " 12 "

Writing pointer type properly in C++ … int *a, *b; a, b are both integer pointers int* a, b; a is integer pointer, b is just integer! typedefine int* IntPt; IntPt a, b; typedefine int MyInt; MyInt k; int k; int* a; int* b; ? Recommended!!! I don’t like this!

Summary * has two usages: - pointer type definition: int a; int* p; - dereferencing: *p is an integer variable if p = &a; & has two usages: - getting address: p = &a; - reference: int& b a; b is an alternative name for a First application in passing parameters (‘swap’ example) int a=10; int b=100; int* p; int* q; P = &a; Q = &b; p = q; *p = *q; ? ?

Pointers and References Reference (implemented as a (const) pointer) is an abstraction, Not available in C, only in C++.

 A pointer can be assigned a new value to point at a different object, but 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. int* p; int m = 10; int& j = m; //valid p = &m; //p now points at m int n = 12; j = n; //the value of m is set to 12. But j still refers to m, not to n. cout << “value of m = “ << m <<endl; //value of m printed is 12 n = 36; Cout << “value of j = “ << j << endl; //value of j printed is 12 p = &n; Pointer vs. Reference

 A reference variable is different from a pointer int x=10; int* ref; Ref = &x; int x=10; int& ref = x; x ref 10 int& ref; 10 x ref

Traditional Pointer Usage void swap(char* ptr1, char* ptr2){ char temp = *ptr1; *ptr1 = *ptr2; *ptr2 = temp; } int main() { char a = 'y'; char b = 'n'; swap(&a, &b); cout << a << b << endl; return 0; } Uese pass-by-value of pointers to ‘change’ variable values C language does not have ‘call by reference’!

Pass by Reference (better than ‘pointers’) void swap(char& y, char& z) { char temp = y; y = z; z = temp; } int main() { char a = 'y'; char b = 'n'; swap(a, b); cout << a << b << endl; return 0; } y, z are ‘references’, only names, not like ptr1, ptr2 that are variables

Pointers and Arrays Double faces of an array: int a[10] l a is the name of an array, l a is also is a constant pointer to its first element

Pointers and Arrays The name of an array points only to the first element not the whole array a[4] a[0] a[2] a[1] a[3] a

Result is: 2 #include Using namespace std; void main(){ int a[5] = {2,4,6,8,22}; cout << *a << " " << a[0] << " " << *(&a[0]);..." } //main a[4] a[0] a[2] a[1] a[3] Dereference of an array name This element is called a[0] or *a

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 Using namespace std; void main(){ int a[5] = {2,4,6,8,22}; int* p = a; int i = 0; cout << a[i] << " " << *p;... } a[4] a[0] a[2] a[1] a[3] a p a Array name as pointer 2 2

dynamic objects

Static variables (objects)Dynamic variables (objects) A (direct) named memory locationA static part (pointer) + (indirect) nameless memory location (dynamic part) int a; a = 20; int* pa; pa = new int; *pa = 20; 20 a pa static dynamic Summary

int* p = new int; *p = 10; delete p; p 10 p int* p = new int[100]; for (i=1;i<100;i++) p[i] = 10; delete[] p; Simple dynamic variable Dynamic array ‘delete p’ is not sufficient for an array!!! ‘delete’ two actions: 1. Return the object pointed to 2. Point the pointer p to NULL 10