Pointers Revisited l What is variable address, name, value? l What is a pointer? l How is a pointer declared? l What is address-of (reference) and dereference.

Slides:



Advertisements
Similar presentations
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Advertisements

Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
A C LOSER L OOK AT C LASSES 1. A SSIGNING O BJECTS One object can be assigned to another provided that both objects are of the same type. It is not sufficient.
Informática II Prof. Dr. Gustavo Patiño MJ
Chapter 15 Memory Management: Four main memory areas for a C++ program: Code: code for instructions, methods, etc. static data: Global variables (declared.
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.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Classes Separating interface from implementation
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.
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.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Review of C++ Programming Part II Sheng-Fang Huang.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
Pointer Data Type and Pointer Variables
CS212: Object Oriented Analysis and Design Lecture 12: Operator Overloading-II.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
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.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
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.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Object-Oriented Programming in C++
Concordia TAV 2002 Comp5421_421 Comp5421 Object Oriented Programming Using C++ Efficiently Lecture 4 (2) Tianxiang Shen Summer 2002 Department of Computer.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Slide 1 Chapter 10 Pointers and Dynamic Arrays. Slide 2 Learning Objectives  Pointers  Pointer variables  Memory management  Dynamic Arrays  Creating.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
CS 1704 Introduction to Data Structures and Software Engineering.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
Engineering Classes. Objectives At the conclusion of this lesson, students should be able to: Explain why it is important to correctly manage dynamically.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Dynamic Memory Review l what is static, automatic, dynamic variables? Why are dynamic(ally allocated) variables needed l what is program stack? Function.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Data Structures in C++ Pointers & Dynamic Arrays Shinta P.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Learners Support Publications Constructors and Destructors.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
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 and References. Pointers & Memory 0x000x040x080x0B0x100x140x180x1B0x20.
Memory Management.
Constructors and Destructors
CS Computer Science IB: Object Oriented Programming
Pointers Revisited What is variable address, name, value?
Dynamic Memory Review what is static, automatic, dynamic variables? why are dynamic(ally allocated) variables needed what is program stack? function.
Dynamically Allocated Memory
Dynamic Memory Allocation
Pointers, Dynamic Data, and Reference Types
Constructors and Destructors
9-10 Classes: A Deeper Look.
Dynamic Memory.
Pointers and References
9-10 Classes: A Deeper Look.
Classes and Objects Object Creation
SPL – PS3 C++ Classes.
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Pointers Revisited l What is variable address, name, value? l What is a pointer? l How is a pointer declared? l What is address-of (reference) and dereference operators? l How can a pointer be assigned a value? l How can a value of a memory location referred to by a pointer be accessed? l What is a constant pointer? What is a pointer to a constant? l What is the relationship between array name and a pointer? l What is the operation to move pointer to the next/previous memory location? l What is null pointer? What is lose pointer problem? l Can pointers be used with objects? How can a method be invoked on an object using pointer? what is -> operator? 1

Dynamic Memory Allocation

Why Dynamic Memory l when a variable is declared - a certain memory area (sufficient to hold the variable of this type or class) is allocated l not all memory can be allocated or efficiently allocated at the compilation time n we are manipulating an set of data of arbitrary size –what if we allocate an array too small? –what if we allocate an array too large? l dynamic memory allocation – memory allocation at execution time under the direct control of programmer l dynamic memory allocation is more flexible since the program can claim and release memory as needed and potentially can get as much memory as the computer resources allow l heap - the system structure where the memory is allocated l heap is separate for every program and it is removed when the program finishes 3

new and delete new and delete - operations used to dynamically manipulate memory new - allocates a nameless variable of specified type (or class) and returns a pointer to it int *ip; // declare pointer ip = new int; // ip points to integer var l note that the variable has no name and the only way to access the variable is though as pointer: cin >> *ip;*ip += 20;cout << *ip; new may take a parameter to initialize the variable with ip = new int(5); // 5 assigned to the var delete - releases memory back to heap so that it can be reused delete ip; note - the memory pointed to by ip goes away not the pointer variable itself l memory allocation types n static variable/constant – location known at compile time: literal constants, global variables, allocated in special place for static vars. n automatic variable – with scope: local variables, parameters, temp. variables, allocated on stack dynamic variable - created with new, allocated in heap 4

Memory Leak Problem l note that the pointer that points to the dynamic variable is the only way to access this variable l if the pointer is reassigned the dynamic variable is “lost”. This is called a memory leak problem int *ptr = new int; ptr = new int; // error - memory leak l is this a memory leak? int *ptr1,*ptr2 = new int; ptr1=ptr2; ptr2 = new int; l does this code have a problem? int *ptr = new int; delete ptr; *ptr=5; 5

Pointers and Arrays l array name is equivalent to: base_type * const l that is array name is a pointer that cannot be changed l array name points to the first element of the array l array name can be used as pointer l pointer can be used as an array name int a[10], *p1, *p2; p1=a; // where does p1 point now? p1[2]=5; // which element does p1 access a=p2; // is this legal? p1[20]; // is this legal? 6

Dynamic Arrays l arrays can be created dynamically just as scalar variables. new has to be passed the number of elements of the array int *p1, *p2, num; p1=new int[10]; l the number of array elements need not be constant: cin >> num; p2=new int[num]; p2= new int[0]; // operates correctly, sometimes useful new returns the pointer to the first element of the dynamically allocated array. This pointer can be used just like regular array name: p1[2]=42; l note, unlike regular array name, this pointer can still be changed p1=p2; delete has special syntax for array deallocation: delete [] p1; note that the pointer passed to delete does not have to be the same that receives the value returned by new n how does the computer know how many elements to delete? – part of dynamic array implementation 7

Pointers and Functions l Pointers can be passed as parameters to and returned by functions // passing pointer by value void funcVal(int *p){ *p = 22; p = new int(33); } // passing pointer by reference void funcRef(int *& p){ *p = 44; p = new int(55); } // passing pointer to pointer, superseded by above: seldom used void funcRefRef(int **pp){ **pp = 66; *pp = new int(77); } // returning pointer int *funcRet(){ int *tmp = new int(88); return tmp; } 8

Memory Leak with Dynamic Arrays int *p = new int [5]; for (int i = 0; i < 5; ++i) p[i] = i; p = new int [5]; p ————— these locations cannot be accessed by program p

Dynamic Objects l objects can be allocated dynamically class myclass { public: myclass(int n){data=n;} int getdata() const {return data;} private: int data; }; the object of class myclass can be allocated as follows: myclass *mp1; mp1 = new myclass(5); l when new is passed parameter - constructor is invoked l the object can then be used as regular object: cout getdata(); l and destroyed: delete mp1; 10

Objects Containing Dynamic Members l if we want to create an object that can shrink and grow as needed? class MyClass{ public: MyClass(int); // constructor private: int *d; int size; }; l with the following constructor: MyClass::MyClass(int n){ size=n; d = new int[size]; } l then the following declaration MyClass myobj(5); creates an object containing an array of 10 integer variables MyClass myobj2(10); // 10-element array 11

Destructor however, when myobj goes out of scope, the dynamically allocated array is leaked l destructor is a function that is called (automatically) when object goes out of scope class MyClass{ public: MyClass(int); // constructor ~MyClass(); // destructor private: int *d; int size; }; name of destructor tilde ( ~ ) and name of class MyClass::~MyClass(){ delete [] d; } l no need to deallocate automatic variables l destructor is never called explicitly and does not accept parameters l note that destructor is called on every local object when function finishes 12

Copy Constructor l what if we have this function (note that the object is passed by value): void myfunc (MyClass); if we invoke it as follows myfunc(myobj); what happens? l the function is passed a reference to the same array - copy of the array is not created l copy constructor is invoked automatically when a new copy of an object is implicitly created n when function is passed an object by value n when function returns an object l copy constructor can be used explicitly l copy constructor is not called when one object is assigned to another (more on that later) 13

Copy Constructor (cont.) l copy constructor is a constructor that accepts an object of the same class passed by reference class MyClass{ public: MyClass(int); // regular constructor MyClass(const MyClass&); // copy constructor private: int *d; int size; }; l defined as follows MyClass::MyClass(const MyClass& org){ size=org.size; d = new int[size]; for(int i=0; i< size; ++i) d[i]=org.d[i]; } l note that copy constructor can access private members of the parameter object can be invoked explicitly: MyClass newobj(myobj); 14

Assignment Overloading what do you think this operation does? secondObj=firstObj; l copies the value of pointer leaking the array of the old object and not creating a copy of the array in the new object l assignment needs to be overloaded l assignment overloading operator accepts by reference the object on the right-hand- side of equation and is invoked by the object on the left-hand-side class MyClass{ public: … void operator= (const MyClass&); private: int *d; int size; }; l can define as follows (not quite right yet): void MyClass::operator= (const MyClass& rhs){ size=rhs.size; delete [] d; d=new int[size]; for (int i=0; i < size; ++i) d[i]=rhs.d[i]; } 15

Self-Assignment Protection what happens if you do this assignment? myobj=myobj; It is legal in C++. Is our overloaded assignment going to handle it right? this is a reserved keyword. It is a pointer to the object that invokes the member function. It is passed implicitly to every member function l assignment overloading (still not quite right): void MyClass::operator= (const MyClass& rhs){ if (this != &rhs){ // if not same size=rhs.size; delete [] d; d=new int[size]; for (int i=0; i < size; i++) d[i]=rhs.d[i]; } } 16

Stackable Assignment l what happens if you do this assignment? thirdObj = secondObj = firstObj; here is the definition that gets above code to work correctly MyClass& MyClass::operator= (const MyClass& rhs){ if (this != &rhs){ // if not same size=rhs.size; delete [] d; d=new int[size]; for (int i=0; i < size; i++) d[i]=rhs.d[i]; } return *this; // return lhs } l note the return value of the function, it is a reference to an object n returned object refers to object used in return-statement n similar to pass-by-reference parameter n seldom used in C++, more common in pointerless-languages (Java, C#) 17

The BIG Three l big three - copy constructor, overloaded assignment and destructor l expert opinion - if you need any one of them - most probably you will need all three l they are not syntactically related but it is usually safer to define all three if you think you’d need at least one 18

Dynamic Memory Review l What is static, automatic, dynamic variables? Why are dynamic(ally allocated) variables needed l What is program stack? Function (invocation) frame? l What is a heap? how is it used? What is new and delete operations? How are they used? l How does one access a dynamically allocated variable? How does one assign a value to such variable at declaration time? l What is memory leak? l How are dynamic arrays allocated? deallocated? l How can a pointer be passed by value/by reference to a function? l What are dynamically allocated objects? Objects containing dynamically allocated members? What may be potential problems with the latter? l What are the big three operations? l What is a destructor? Why is it needed? When is it executed? How is it declared/defined? l What is a copy-constructor? Why is it needed? When is it executed? How is it declared/defined? l What is operation overloading? What is overloaded assignment? Why is it needed for objects with dynamic members? How is it declared/defined? What is this -pointer? What is protection against self-assignment? How is this -pointer used for that? l What is stackability of an operator? How can overloaded assignment be made stackable? 19