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.

Slides:



Advertisements
Similar presentations
Data Structures Using C++ 2E
Advertisements

Chapter 6 Data Types
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
Informática II Prof. Dr. Gustavo Patiño MJ
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.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Chapter 9. 2 Objectives You should be able to describe: Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 10: Pointers by.
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. 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.
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
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.
Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 10: 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.
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
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Pointer Data Type and Pointer Variables
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified by use by the MSU CMPS Dept. Chapter 10:
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
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.
Basic Semantics Associating meaning with language entities.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Chapter 9 Pointers and Dynamic Arrays (9.1). Pointers A variables which holds the memory address for a variable of a specific type. Call-by-Reference.
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.
Object-Oriented Programming in C++
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Pointer and Array Lists Chapter 3, Summary CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
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.
Pointers and Dynamic 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:
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.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
Lecture 10: 2/17/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
1 Memory as byte array Pointers Arrays relationship to pointers Operator ‘new’ Operator ‘delete’ Copy ctor Assignment operator ‘this’ const pointer Allocating.
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.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
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.
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
Object Lifetime and Pointers
Dynamic Storage Allocation
Pointers.
Chapter 10: Pointers Starting Out with C++ Early Objects
Pointers Revisited What is variable address, name, value?
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Chapter 10: Pointers Starting Out with C++ Early Objects
Dynamic Memory Allocation
Pointers, Dynamic Data, and Reference Types
Chapter 12 Pointers and Memory Management
Dynamic Memory.
ENERGY 211 / CME 211 Lecture 10 October 13, 2008.
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

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 elements to change capacity, we change MAX_SIZE in the header then we recompile the program – not pretty! We get around this using dynamic memory allocation memory is given to an object at run-time each object gets what it needs as it needs it dynamic memory requires the use of C++ pointers

CPSC 252 Dynamic Memory Allocation Page 2 Pointers A pointer is a data type whose value is the address of an object in memory char* charPtr; char myChar = ‘A’; We “point to” an object using the & or “address of” operator: charPtr = &myChar; We can change the value of myChar indirectly using the * or “dereferencing” operator: *charPtr = ‘B’; charPtr myChar ‘A’ charPtr myChar ‘B’ charPtr myChar ‘A’ ???

CPSC 252 Dynamic Memory Allocation Page 3 charPtr char2char1 ‘B’ ‘A’ charPtr char2char1 ‘B’ ‘C’ Constant pointers and constant objects char char1 = ‘A’; char char2 = ‘B’; char* const charPtr = &char1; the variable charPtr is a constant pointer to char the address in charPtr must be defined by initialization the address in charPtr cannot be changed afterward charPtr = &char2; you can change the object pointed to *charPtr = ‘C’;

CPSC 252 Dynamic Memory Allocation Page 4 Pointers to constant objects char char1 = ‘A’; const char char2 = ‘B’; const char* charPtr = &char1; the variable charPtr is a pointer to a constant char the address in charPtr can be changed any time the character pointed to cannot be changed *charPtr = ‘C’; char2 = ‘D’; you can change the object pointed to and char1 itself charPtr = &char2; char1 = ‘E’; charPtr char2char1 ‘B’ ‘A’ charPtr char2char1 ‘B’ ‘E’

CPSC 252 Dynamic Memory Allocation Page 5 Reference types char myChar = ‘A’; char myChar = ‘B’; char& charRef1 = myChar1; const char& charRef2 = myChar2; “like” a constant pointer, but better! charRef1 is a reference to the character myChar1 it stores the address of the variable that it is referencing this address cannot be changed once it has been assigned automatic dereference (no * operator ) charRef1 = charRef2; but not for a constant reference charRef2 = ‘C’; charRef1 myChar1 ‘A’ charRef2 myChar2 ‘B’ charRef2 myChar2 ‘B’ charRef1 myChar1 ‘B’

CPSC 252 Dynamic Memory Allocation Page 6 Reference parameters Reference variables are often used as function parameters formal parameter become references to actual parameters (more about paramter-passing in a future lecture) void swap( int& num1, int& num2 ) { int temp; temp = num1; num1 = num2; num2 = temp; } int x = 2, y = 5; swap( x, y );

CPSC 252 Dynamic Memory Allocation Page 7 Dynamic memory allocation (after learning pointers) C++ has three kinds of variables: automatic variables allocated and de-allocated as needed static variables allocated for the lifetime of the execution dynamic variables allocated and de-allocated explicitly during execution as specified by the program Dynamically memory allocation allows: memory efficiently (use only what you need) array (and object) sizes that vary at run time more sophisticated data structures and algorithms

CPSC 252 Dynamic Memory Allocation Page 8 The new operator new returns the address of a newly allocated object int* intPtr; intPtr = new int; We can also allocate an array of integers: int capacity; cout > capacity; int* data = new int[ capacity ]; for( int index = 0; index > data[ index ]; intPtr data ???

CPSC 252 Dynamic Memory Allocation Page 9 Garbage collection Dynamic memory must be released when no longer required some languages have automatic garbage collection C++ requires explicit garbage collection by the program memory is released using the delete operator: delete intPtr; delete[] data; delete operator does not delete the pointer it releases the memory to which the pointer points delete operator will not change the pointer it still points to the memory that was just released! this is a problem

CPSC 252 Dynamic Memory Allocation Page 10 Dangling pointers An illegal pointer dereference results if the pointer is: not initialized (it could point anywhere) NULL (its value is zero so it points nowhere) referencing an object that no longer exists (dangling pointer) referencing outside the object it points to Pointer arithmetic causes the last case: int* p = new int[3]; *(p + 4) = p[5]; both attempts dereference outside the dynamic array so they are illegal pointer dereferences

CPSC 252 Dynamic Memory Allocation Page 11 Avoiding dangling pointers After using the delete operator: it is often a good idea to set the corresponding pointer to 0 the built-in value NULL is equal to zero delete intptr; intptr = NULL; this avoids accidentally dereferencing a dangling pointer A pointer whose value is NULL is a null pointer we often use a “ground” symbol to denote a NULL pointer intptr

CPSC 252 Dynamic Memory Allocation Page 12 How to avoid dangling pointers release memory only when there is only one pointer to it! int* intPtr1; int* intPtr2; intPtr1 = new int; *intPtr1 = 7; intPtr2 = intPtr1; delete intPtr1; intPtr1 = NULL; intPtr2intPtr1 7 intPtr2intPtr1 ??? intPtr2intPtr1 ??? intPtr2intPtr1 ??? intPtr2intPtr1 ??? intPtr2intPtr1 7 ???

CPSC 252 Dynamic Memory Allocation Page 13 Memory leaks If there is no pointer pointing to a dynamically allocated block of memory, there is a memory leak int* intPtr; intPtr = new int; *intPtr = 7; intPtr = new int; *intPtr = 5; intPtr ??? intPtr 7 7 5

CPSC 252 Dynamic Memory Allocation Page 14 Invoking delete with and without the [] What memory is released when a pointer is to an array? int* data = new int[10]; delete [] data; delete operator releases all 10 words of memory int* data = new int[10]; delete data; delete operator still releases all 10 words of memory but only one destructor is invoked (for the first object) The only difference is whether 1 or 10 destructors are called