Pointers & Dynamic Memory

Slides:



Advertisements
Similar presentations
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.
Advertisements

Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Introduction to Programming Lecture 39. Copy Constructor.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 10: Pointers by.
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Memory Allocation Ming Li Department.
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
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.
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.
CMSC 202 Lesson 13 Pointers & Dynamic Memory. Warmup Overload the subtraction operator on two Money objects as a FRIEND! class Money { public: _______________.
Pointers OVERVIEW.
CMSC 202 Lesson 14 Copy and Assignment. Warmup Write the constructor for the following class: class CellPhone { public: CellPhone(int number, const string&
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.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Data Structures Using C++1 Chapter 3 Pointers Dr. Liu.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
P OINTERS A pointer is an address All data is stored in memory in some location that is indexed with an address Can refer to variables by name or by memory.
Pointers and Dynamic Arrays
CMSC 341 Lecture 2 – Dynamic Memory and Pointers (Review)
Day 03 Introduction to C.
CS 215 Final Review Ismail abumuhfouz Fall 2014.
Chapter 9: Pointers.
Motivation and Overview
Chapter 10: Pointers Starting Out with C++ Early Objects
Lesson 14 Copy and Assignment
Day 03 Introduction to C.
Pointers Revisited What is variable address, name, value?
Dynamic Memory CSCE 121 J. Michael Moore.
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
CSCE 210 Data Structures and Algorithms
Chapter 10: Pointers Starting Out with C++ Early Objects
LinkedList Class.
This pointer, Dynamic memory allocation, Constructors and Destructor
CMSC 202 Lesson 21 Exceptions II.
Pointers and References
Dynamic Memory Allocation
Object Oriented Programming COP3330 / CGS5409
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 9: Pointers.
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Pointers, Dynamic Data, and Reference Types
Constant pointers and pointers to constants
Exceptions 2 CMSC 202.
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.
Indirection.
Dynamic Memory A whole heap of fun….
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Chapter 16 Linked Structures
Destructor CSCE 121 J. Michael Moore.
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 A whole heap of fun….
C Programming Lecture-8 Pointers and Memory Management
Chapter 9: Pointers and String
Data Structures and Algorithms Memory allocation and Dynamic Array
Pointers and References
Pointers and References
Dynamic Memory CSCE 121.
Destructors, Copy Constructors & Copy Assignment Operators
Rule of Three Part 1 & 2.
Pointers, Dynamic Data, and Reference Types
Destructors, Copy Constructors & Copy Assignment Operators
Variable Storage Memory Locations (Logical) Variable Classes Stack
SPL – PS2 C++ Memory Handling.
Lesson 13 Pointers & Dynamic Memory
Presentation transcript:

Pointers & Dynamic Memory CMSC 202

Representing Variables Regular variables int age; Array of ints int ages[4]; Struct with 2 data pieces struct Person { char initial; }; Person p; Array of structs Person people[4]; int: age int[]: ages int: p.age char: p.initial Person: p Person[]: people

Pointer Review Creating a pointer Connecting it to a pointee int* ptr; Connecting it to a pointee int a = 4; ptr = &a; Changing its pointee’s value *ptr = 7; Changing pointees int b = 2; ptr = &b; int*: ptr int*: ptr int: a 4 int*: ptr int: a 7 int*: ptr int: a 7 int*: ptr int: b 2

Pointer Operators & * = Examples: int: a 3 8 int*: ptr Address of pointee Syntax: type* ptr = &variable; ptr = &variable2; * Dereferencing, Value of pointee *ptr = value; variable = *ptr; = Assignment, point to something else ptrA = ptrB; Examples: int a = 3; int* ptr = &a; *ptr = 8; int b = 5; int* ptr2 = &b; ptr = ptr2; int: a 3 8 int*: ptr

Arrays and Pointer Arithmetic Tricky stuff… Arrays are simply a kind of pointer Points to first item in collection Index into array is “offset” Example int ages[4] = {0, 1, 2, 3}; int* ptr = &ages[2]; *ptr = 8; ptr++; *(ptr - 2) = 9; int*: ptr 9 1 8 2 3 int[]: ages ptr - 2

Dynamic Memory and Classes Types of memory from Operating System Stack – local variables and pass-by-value parameters are allocated here Heap – dynamic memory is allocated here C malloc() – memory allocation free() – free memory C++ new – create space for a new object (allocate) delete – delete this object (free)

New Objects new Syntax: Works with primitives Works with class-types type* ptrName = new type; type* ptrName = new type( params ); Constructor!

Pointers are the same size no matter how big the data is! New Examples Notice: These are unnamed objects! The only way we can get to them is through the pointer! Pointers are the same size no matter how big the data is! int* intPtr = new int; Car* carPtr = new Car(“Nissan”, “Pulsar”); Customer* custPtr = new Customer; int*: intPtr int: Car*: carPtr Car: Nissan Pulsar Customer: Customer*: custPtr

Deletion of Objects delete Syntax: Example: Called on the pointer to an object Works with primitives & class-types Syntax: delete ptrName; Example: delete intPtr; intPtr = NULL; delete carPtr; carPtr = NULL; delete custPtr; custPtr = NULL; Set to NULL so that you can use it later – protect yourself from accidentally using that object!

Pointer Fun with Binky http://cslibrary.stanford.edu/104/ Video! Pointer Fun with Binky http://cslibrary.stanford.edu/104/

Practice Assume you have a Shoe class: Create a pointer to a Shoe Connect the pointer to a new Shoe object Delete your Shoe object Set pointer to null Shoe* shoePtr; shoePtr = new Shoe; delete shoePtr; shoePtr = NULL;

Dynamic Arrays?! Syntax Example type* arrayName = new type[ size ]; type* arrayName = new type[ size ] ( params ); delete [ ] arrayName; Example int* intPtr; intPtr = new int[ 5 ]; Car* carPtr; carPtr = new Car[ 10 ]; Customer* custPtr; custPtr = new Customer[ 3 ]; int*: intPtr Constructor!

Dynamic Arrays?! Syntax Example type* arrayName = new type[ size ]; type* arrayName = new type[ size ] ( params ); delete [ ] arrayName; Example int* intPtr; intPtr = new int[ 5 ]; Car* carPtr; carPtr = new Car[ 10 ] ( “Nissan”, “Pulsar” ); Customer* custPtr; custPtr = new Customer[ 3 ]; int*: intPtr Constructor!

Dynamic 2D Arrays char**: chArray2 Algorithm Example Allocate the number of rows For each row Allocate the columns Example const int ROWS = 3; const int COLUMNS = 4; char **chArray2; // allocate the rows chArray2 = new char* [ ROWS ]; // allocate the (pointer) elements for each row for (int row = 0; row < ROWS; row++ ) chArray2[ row ] = new char[ COLUMNS ]; char**: chArray2

Dynamic 2D Arrays Delete? Example // delete the columns Reverse the creation algorithm For each row Delete the columns Delete the rows Example // delete the columns for (int row = 0; row < ROWS; row++) { delete [ ] chArray2[ row ]; chArray2[ row ] = NULL; } // delete the rows delete [ ] chArray2; chArray2 = NULL;

2D Vectors?! Allocation Deletion vector< vector< int > > intArray; Deletion // allocate the rows intArray.resize ( ROWS ); // allocate the columns for (unsigned int i = 0; i < intArray.size( ); i++) intArray[ i ].resize( COLUMNS ); Notice the space, why??

Destructors Constructors Destructors Syntax: class ClassName { public: Construct or create the object Called when you use new Destructors Destroy the object Called when you use delete Why is this needed? Dynamic memory WITHIN the class! Syntax: class ClassName { public: ClassName(); // Constructor ~ClassName(); // Destructor // other stuff… };

Destructor Example class Car { public: private: }; Car( const string& make, int year); ~Car(); // Destructor private: string* m_make; int* m_year; }; Car::Car( const string& make, int year) { m_make = new string(make); m_year = new int(year); } Car::~Car() delete m_make; m_make = NULL; // cleanup delete m_year; m_year = NULL; // cleanup

Dynamic Memory Rules Classes Delete If dynamic data MUST have constructor MUST have destructor Delete After delete – always set pointer to NULL Security “For every new, there must be a delete.”

Practice Dynamically create an array of 50 Shoes Delete your array of shoes “Clear” the pointer Shoe* shoeArray = new Shoe[ 50 ]; delete shoeArray; shoeArray = NULL;

Challenge Create a very simple Car class Dynamically allocate an array of Passengers within the car Create a constructor to allocate the array Create a deconstructor to delete the array