Pointers and References. Pointers & Memory 0x000x040x080x0B0x100x140x180x1B0x20.

Slides:



Advertisements
Similar presentations
Chapter 18 Vectors and Arrays
Advertisements

Chapter 18 Vectors and Arrays John Keyser’s Modification of Slides by Bjarne Stroustrup
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
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.
Introduction to Programming Lecture 39. Copy Constructor.
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.
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.
Writing a Good Program 6. Pointers and Arrays
A RRAYS, P OINTERS AND R EFERENCES 1. A RRAYS OF O BJECTS Arrays of objects of class can be declared just like other variables. class A{ … }; A ob[4];
C++ Mini-Course Part 1: Mechanics Part 2: Basics Part 3: References Part 4: Const Part 5: Inheritance Part 6: Libraries Part 7: Conclusion C++ Rulez!
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
1 CSE 303 Lecture 11 Heap memory allocation ( malloc, free ) reading: Programming in C Ch. 11, 17 slides created by Marty Stepp
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Class Constructors a class constructor is a member function whose purpose is to initialize the private data members of a class object the name of a constructor.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Pointer Data Type and Pointer Variables
February 11, 2005 More Pointers Dynamic Memory Allocation.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
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.
CS 11 C++ track: lecture 4 Today: More on memory management the stack and the heap inline functions structs vs. classes.
1 Data Structures - CSCI 102 CS102 C++ Pointers & Dynamic Objects Prof Tejada.
C++ Memory Overview 4 major memory segments Key differences from Java
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 13: Pointers You are not responsible for virtual functions (starting on.
1 Chapter 15-2 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
 Managing the heap  Resource acquisition is initialization (RAII)  Overriding operator new and delete  Class-based memory pools.
Dynamically Allocated Arrays December 4, Skip the Rest of this PowerPoint.
Object-Oriented Programming in 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.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
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.
C++ REVIEW – POINTERS AND TEST DRIVEN DEVELOPMENT.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
C++ Review Part 1: Mechanics Part 2: Basics Part 3: References Part 4: Const Part 5: Inheritance Part 6: Libraries Acknowledgement: Adapted from: Brown.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
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:
Dynamic Memory Review l what is static, automatic, dynamic variables? Why are dynamic(ally allocated) variables needed l what is program stack? Function.
1 // SPECIFICATION FILE (dynarray.h) // Safe integer array class allows run-time specification // of size, prevents indexes from going out of bounds, //
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
POINTERS AND MEMORY ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
Yan Shi CS/SE 2630 Lecture Notes
Pointers and Dynamic Arrays
Overview 4 major memory segments Key differences from Java stack
Pointers Revisited What is variable address, name, value?
Dynamic Memory CSCE 121 J. Michael Moore.
Memberwise Assignment / Initialization
Dynamic Memory Review what is static, automatic, dynamic variables? why are dynamic(ally allocated) variables needed what is program stack? function.
group work #hifiTeam
Dynamically Allocated Memory
Dynamic Memory Allocation Reference Variables
Dynamic Memory Allocation
CSC 253 Lecture 8.
CSC 253 Lecture 8.
Overview 4 major memory segments Key differences from Java stack
understanding memory usage by a c++ program
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
Dynamic Memory A whole heap of fun….
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers And Memory Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
Class and Objects In a class, all the functions that operate on the data structure are grouped together in one place along with the data Like a struct.
Destructor CSCE 121.
Dynamic Memory.
Dynamic Memory Copy Challenge
Pointers and References
Dynamic Memory CSCE 121.
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

Pointers and References

Pointers & Memory 0x000x040x080x0B0x100x140x180x1B0x20

5 int x = 5; x 0x000x040x080x0B0x100x140x180x1B0x20 Pointers & Memory

5 int x = 5; int* y = &x xy 0x04 0x000x040x080x0B0x100x140x180x1B0x20

Pointers & Memory 5 int x = 5; int* y = &x int* z = y; xy 0x04 z 0x000x040x080x0B0x100x140x180x1B0x20

Pointers & Memory 0 int x = 5; int* y = &x int* z = y; *z = 0; xy 0x04 z 0x000x040x080x0B0x100x140x180x1B0x20

Allocating memory using new Point *p = new Point(5, 5); new can be thought of a function with slightly strange syntax new allocates space to hold the object. new calls the object’s constructor. new returns a pointer to that object.

Deallocating memory using delete // allocate memory Point *p = new Point(5, 5);... // free the memory delete p; For every call to new, there must be exactly one call to delete.

Using new with arrays int x = 10; int* nums1 = new int[10]; // ok int* nums2 = new int[x]; // ok Initializes an array of 10 integers on the heap. C++ equivalent of the following C code int* nums = (int*)malloc(x * sizeof(int));

Using delete on arrays // allocate memory int* nums1 = new int[10]; int* nums3 = new int[x][4][5];... // free the memory delete[] nums1; delete[] nums3; Have to use delete[].

Destructors delete calls the object’s destructor. delete frees space occupied by the object. A destructor cleans up after the object. Releases resources such as memory.

Destructors – an Example class Segment { public: Segment(); virtual ~Segment(); private: Point *m_p0, *m_p1; };

Destructors – an Example Segment::Segment() { m_p0 = new Point(0, 0); m_p1 = new Point(1, 1); } Segment::~Segment() { if (m_p0) delete m_p0; if (m_p1) delete m_p1; }

Syntactic Sugar “ -> ” Point *p = new Point(5, 5); // Access a member function: (*p).move(10, 10); // Or more simply: p->move(10, 10);

Stack vs. Heap On the Heap / Dynamic allocation On the Stack / Automatic allocation Point *p = new Point(); Point *ps = new Point[n]; Point p; Point ps[10]; What happens when p goes out of scope?

Dynamic Memory S

Dynamic Memory S T int* T = new int[2*n];

Dynamic Memory S T for (i = 0; i < n; i++) T[i] = S[i];

Dynamic Memory S T delete[] S;

Dynamic Memory S T S = T;

Passing by value void Math::square(int i) { i = i*i; } int main() { int i = 5; Math::square(i); cout << i << endl; }

Passing by reference void Math::square(int &i) { i = i*i; } int main() { int i = 5; Math::square(i); cout << i << endl; }

What is a reference? An alias – another name for an object. int x = 5; int &y = x; // y is a // reference to x y = 10;

Introducing: const void Math::printSquare(const int &i){ i = i*i; cout << i << endl; } int main() { int i = 5; Math::printSquare(i); Math::printCube(i); } Won’t compile.

Can also pass pointers to const void Math::printSquare(const int *pi) { *pi = (*pi) * (*pi); cout << pi << endl; } int main() { int i = 5; Math::printSquare(&i); Math::printCube(&i); } Still won’t compile.

Declaring things const const River nile; const River* nilePc; River* const nileCp; const River* const nileCpc

Read pointer declarations right to left // A const River const River nile; // A pointer to a const River const River* nilePc; // A const pointer to a River River* const nileCp; // A const pointer to a const River const River* const nileCpc

Exercises 1.Create a class called Point with two private data members, a public print function, and constructors as well as a destructor that prints a message when it is called. 2.Create an array of 10 Points on the stack, initialize them with non- zero data, and print them. Verify that that the destructor is called for all 10 points when your program exits. 3.Create an array of 10 Points on the heap, initialize them with non- zero data, and print them. Do not delete the array and verify the destructor is not called (you have created a memory leak). Now write your program to delete the array at the end. Verify that only one destructor is called and your program crashes (you have created a memory leak and a bug). Now write your program to delete[] the array at the end. Verify that all destructors are called.