Revision on C++ Pointers TCP1201: 2013/2014. Pointer Basics  Why pointer is important? 1. Reference/Point to existing data without cloning the data.

Slides:



Advertisements
Similar presentations
Derived data types Dealing with data –Where the information is stored –What value is kept there –What kind of information is stored Address operator Pointers.
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.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
DYNAMIC MEMORY MANAGEMENT. int x; When the source code containing this statement is compiled and linked, an executable file is generated. When the executable.
Informática II Prof. Dr. Gustavo Patiño MJ
1 Pointers (Walls & Mirrors - Beginning of Chapter 4)
Dynamic Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
Dynamic Objects. COMP104 Lecture 31 / Slide 2 Static verses Dynamic Objects * Static object n Memory is acquired automatically  int A[10]; n Memory is.
Pointers “Absolute C++” Section 10.1
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.
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.
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Self Referential Structure. A structure may not contain a member of its own type. struct check { int item; struct check n; // Invalid };
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Pointers. Overview  What are Pointers?  How to use Pointers?  Use of 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. This is.
February 11, 2005 More Pointers Dynamic Memory Allocation.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Dynamic Memory Allocation 9.8.
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.
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.
TCP1201 OOPDS 1 Class & Template Lecture 2. TCP1201 OOPDS 2 Learning Objectives: To understand object behaviors To understand constructors To understand.
1 Data Structures CSCI 132, Spring 2014 Lecture 10 Dynamic Memory and Pointers.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
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.
Dynamically Allocated Arrays December 4, Skip the Rest of this PowerPoint.
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.
Dynamic memory allocation and Pointers Lecture 4.
Dynamic Allocation Joe Meehean. Dynamic Allocation Memory for local objects is automatically created and reclaimed memory is created for it at beginning.
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.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Chapter 7 Pointers: Java does not have pointers. Used for dynamic memory allocation.
Review 1 List Data Structure List operations List Implementation Array Linked List.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
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.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
Pointers and Arrays Dynamic Variables and Arrays.
CSCI 125 & 161 / ENGR 144 Lecture 16 Martin van Bommel.
Pointers & Arrays.
Introduction to Programming
Linked lists.
C++ Interlude 2 Pointers, Polymorphism, and Memory Allocation
Dynamic Memory CSCE 121 J. Michael Moore.
Pointers Psst… over there.
Dynamic Memory Allocation Reference Variables
Pointers Psst… over there.
Pointer Basics Psst… over there.
Dynamic Memory A whole heap of fun….
Pointers & Functions.
Pointers And Memory Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
Dynamic Memory A whole heap of fun….
Dynamic Memory A whole heap of fun….
Pointers Lecture 1 Thu, Jan 15, 2004.
Dynamic Memory A whole heap of fun….
Dynamic Memory.
C Programming Lecture-8 Pointers and Memory Management
Pointers & Arrays.
The Stack.
Pointer Basics Psst… over there.
Linked lists.
Dynamic Memory CSCE 121.
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

Revision on C++ Pointers TCP1201: 2013/2014

Pointer Basics  Why pointer is important? 1. Reference/Point to existing data without cloning the data. 2. Dynamic memory allocation (create the amount of data based on runtime need). 3. Dynamic polymorphism (Lecture 4)  A pointer always stores the address of a data.  Before we can use a pointer, it must point to a valid address that is achieved through:  Pointing it to an existing data, or  Using it to create a new data (use new operator).

3 int a = 3; int *p = &a; cout << *p; // 3 cout << p; // 1432 The ampersand '&' is called address operator which returns the address of variable 'a'. 'a' stores an int. 'p' stores the address of variable 'a'. Point to Data Without Cloning the Data p [ int* ] 3 a [ int ] FB5 We say 'p' points to 'a'. 'p' is not a clone of 'a'. p [ int * ]

Pointers Basics int a = 3; [int] FA0 p [ int* ] a = 10; cout << a << endl; cout << *p << endl << endl; *p = 99; cout << a << endl; cout << *p << endl << endl; 1099 a int *p; p = &a; cout << a << endl; cout << *p << endl << endl;

Pointers Basics int a[5] = {3, 6, 9, 12, 15}; int *p = a; cout << a[0] << endl; cout << *p << endl << endl; for (int i=0; i<5; i++) cout << a[i] << " "; cout << endl; for (int i=0; i<5; i++) cout << p[i] << " "; cout << endl << endl; [int] FA0 p [ int* ] FB4 a [int]

Pointers Basics cout << (*p)++ << endl; cout << *p << endl << endl; for (int i=0; i<5; i++) cout << a[i] << " "; cout << endl; for (int i=0; i<5; i++) cout << p[i] << " "; cout << endl << endl; FA0 p [ int* ] 12 [int] FB4 a [int]

Pointers Basics cout << (*p)++ << endl; cout << *p << endl << endl; for (int i=0; i<5; i++) cout << a[i] << " "; cout << endl; for (int i=0; i<5; i++) cout << p[i] << " "; cout << endl << endl; FA0 p [ int* ] 12 [int] FB4 a [int]

Pointers Basics cout << *p++ << endl; cout << *p << endl << endl; for (int i=0; i<5; i++) cout << a[i] << " "; cout << endl; for (int i=0; i<5; i++) cout << p[i] << " "; cout << endl << endl; FA0 p [ int* ] 12 [int] FB4 a [int]

Pointers Basics cout << *p++ << endl; cout << *p << endl << endl; for (int i=0; i<5; i++) cout << a[i] << " "; cout << endl; for (int i=0; i<5; i++) cout << p[i] << " "; cout << endl << endl; FA0 p [ int* ] 12 [int] FB4 a [int]

Is pointer that does not point to an valid address. Use of dangling pointer generates runtime error easily. 10 int* p; // 'p' usually does not point to // a valid address. *p = 10; // Runtime error usually. p [ int* ] ? Dangling Pointers

Dynamic Memory Allocation (DMA)  To create new data dynamically.  We use new operator to create new data, and later use delete operator to release the memory used by the data.  The new operator allocates the memory needed for the new data, and returns the address of the new data. int* p = new int; *p = 10;... delete p; FA0 p [ int* ] [int] 10

Dynamic Memory Allocation (DMA) int* p; p = new int[4]; FA0 p [ int* ] [int]

Dynamic Memory Allocation (DMA) int* p; p = new int[4]; p[0] = 12; p[1] = 24; p[2] = 15; p[3] = 35; FA0 p [ int* ] 35 [int]

Dynamic Memory Allocation (DMA) int* p; p = new int[4]; p[0] = 13; p[1] = 42; p[2] = 15; p[3] = 32; FA0 p [ int* ] [int]... delete[] p; p = NULL;

Review question int main() { int a[] = {11, 22, 33, 44, 55}; int x; int *p = a; x = *p++; cout<< "*p = " << *p <<"\n\n"; cout<< "x = " << x <<"\n\n\n"; int b[] = {11, 22, 33, 44, 55}; int y; int *q = b; y = (*q)++; cout<< "*q = " << *q <<"\n\n"; cout<< "y = " << y <<"\n\n\n"; int c[] = {11, 22, 33, 44, 55}; int z; int *r = c; z = *++r; cout<< "*r = " << *r <<"\n\n"; cout<< "z = " << z <<"\n\n\n"; return 0; }