Dynamic Memory CSCE 121.

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.
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.
Pointer Data Type and Pointer Variables
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.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
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 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.
 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.
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.
POINTERS AND MEMORY ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
Dynamic Storage Allocation
Pointers and Dynamic Arrays
CMSC 341 Lecture 2 – Dynamic Memory and Pointers (Review)
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Pointers, Polymorphism, and Memory Allocation
Linked lists.
Checking Memory Management
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
Pointers Psst… over there.
Dynamically Allocated Memory
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
Chapter 9: Pointers.
Pointers, Dynamic Data, and Reference Types
Dynamic Memory A whole heap of fun….
Pointers And Memory Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
Chapter 12 Pointers and Memory Management
Indirection.
Dynamic Memory A whole heap of fun….
Dynamic Memory A whole heap of fun….
Dynamic Memory Management
Introduction to C++ Linear Linked Lists
Destructor CSCE 121 J. Michael Moore.
Arrays an array of 5 ints is filled with 3,2,4,1,7
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 lists.
Dynamic Memory Management
Pointers, Dynamic Data, and Reference Types
Classes and Objects Object Creation
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Dynamic Memory CSCE 121

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 use the heap? Use ‘new’ Gets memory from the heap Returns a pointer Use ‘*’ to dereference the pointer Initialize with nullptr (i.e. 0) – See Null Pointer note 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; int* k = new int[5]; // allocate an array on the heap

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’ Use ‘delete[]’ if deleting an array 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 ‘->’ Color* c = new Color { 123,233,18 }; cout << "red: " << c->get_red() << endl; cout << "green: " << c->get_green() << 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. Color* c = new Color { 123,233,18 }; cout << "red: " << (*c).get_red() << endl; cout << "green: " << (*c).get_green() << endl;