Dynamic Memory CSCE 121 J. Michael Moore.

Slides:



Advertisements
Similar presentations
Chapter 17 vector and Free Store John Keyser’s Modifications of Slides By Bjarne Stroustrup
Advertisements

Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
CS-1030 Dr. Mark L. Hornick 1 Pointers And Dynamic Memory.
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.
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 and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
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.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
Pointers OVERVIEW.
1 Data Structures CSCI 132, Spring 2014 Lecture 10 Dynamic Memory and Pointers.
C++ Memory Overview 4 major memory segments Key differences from Java
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.
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.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Dynamically Allocated Arrays December 4, Skip the Rest of this PowerPoint.
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.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Revision on C++ Pointers TCP1201: 2013/2014. Pointer Basics  Why pointer is important? 1. Reference/Point to existing data without cloning the data.
 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 
Container Classes. How do we do better? Tough to delete correctly: startLocation.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Motivation and Overview
Pointers, Polymorphism, and Memory Allocation
Linked lists.
Pointers Revisited What is variable address, name, value?
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Pointers Psst… over there.
Today’s Topic Dynamic allocation Slides for CS 31 discussion session
Pointers Psst… over there.
Dynamic Memory Allocation
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Variables with Memory Diagram
Chapter 9: Pointers.
Linked List Intro CSCE 121 J. Michael Moore.
Pointers, Dynamic Data, and Reference Types
Dynamic Memory A whole heap of fun….
Dynamic Memory Copy Challenge
Return by Reference CSCE 121 J. Michael Moore.
Exceptions CSCE 121 J. Michael Moore
Pointers And Memory Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
Chapter 12 Pointers and Memory Management
Dynamic Memory A whole heap of fun….
How Dynamic Memory Works with Memory Diagram
Dynamic Memory A whole heap of fun….
Dynamic Memory Management
Essential Class Operations
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….
Dynamic Memory.
COP 3330 Object-oriented Programming in C++
Linked List Intro CSCE 121.
Essential Class Operations
Linked lists.
Dynamic Memory CSCE 121.
Dynamic Memory Management
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

Dynamic Memory CSCE 121 J. Michael Moore

Memory Recall: Memory Layout Stack and heap grow toward each other. Code Static Data Stack and heap grow toward each other. Heap / Free Store Heap / Free Store Stack Stack

Stack Recall: As stack grows / shrinks, items are automatically cleared from Memory i.e. when a function ends, all of its objects (variables) are cleared from Memory Sometimes we want objects to live on after a function is finished. Put on the Heap / Free Store

Heap aka Dynamic Memory How to add to the heap? Use ‘new’ Use pointers Use ‘*’ to dereference the pointer Initialize with nullptr (i.e. 0) – See Null Pointer note in 3.9 in zyBook. int i = 7; // put item on the stack int* j = nullptr; j = new int(11); // put item on the heap cout << “Value in i: " << i << endl; cout << "Address of i: " << &i << endl; cout << “Value in j: " << j << endl; cout << "Address of j: " << &j << endl; cout << "*j (value at address pointed to in j): " << *j << endl;

Heap If we put something on the heap we also have to remove it. If we don’t we might have a memory leak. More on memory management / challenges with pointers later. How to remove from the heap? Use ‘delete’ delete j; // remove item from the heap // j still points to the memory in the heap // that can be a problem

Notes on new / delete If you delete memory that has already been deleted, then an exception will likely occur. If you delete a pointer that is set to nullptr, then no error occurs. If you try to dereference a pointer that has been deleted, then an exception will likely occur. So try to set the pointer to nullptr after you use delete.

RAII Resource Acquisition Is Initialization (RAII) Try to allocate resources in constructor (i.e. allocate memory from the heap) Try to deallocate resources in destructor (i.e. deallocate memory from the heap) We’ll discuss this more later.

Pointers to Classes & Structs Dereferencing pointers has a special operator (that we’ve already seen.) Use ‘->’ vector<int>* v = new vector<int> { 2,4,6 }; cout << "vector size: " << v->size() << endl; cout << "first value: " << v->at(0) << endl;

Pointers to Classes & Structs -> is equivalent to dereferencing and then using the dot operator Alternatively we could do the following However, this is not normally done. vector<int>* v = new vector<int> { 2,4,6 }; cout << "vector size: " << (*v).size() << endl; cout << “first value: " << (*v).at(0) << endl;