Dynamic Memory A whole heap of fun….

Slides:



Advertisements
Similar presentations
Dynamic memory allocation
Advertisements

Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Informática II Prof. Dr. Gustavo Patiño MJ
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
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.
Dynamic Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
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.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Outline Midterm results Static variables Memory model
February 11, 2005 More Pointers Dynamic Memory Allocation.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
CS 11 C++ track: lecture 4 Today: More on memory management the stack and the heap inline functions structs vs. classes.
Pointers review Let a variable aa be defined as ‘int *aa;’, what is stored in aa? Let a variable aa be defined as ‘int ** aa;’ what is stored in aa? Why.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
1 Writing a Good Program 8. Elementary Data Structure.
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.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
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++
Dynamic memory allocation and Pointers Lecture 4.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Dynamic Allocation Review Structure and list processing
Overview 4 major memory segments Key differences from Java stack
Day 03 Introduction to C.
Arrays Low level collections.
Pointers & Arrays.
Introduction to Programming
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
Day 03 Introduction to C.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Checking Memory Management
Pointers Revisited What is variable address, name, value?
CSCI206 - Computer Organization & Programming
Dynamic Memory CSCE 121 J. Michael Moore.
Chapter 10: Pointers Starting Out with C++ Early Objects Ninth Edition
Dynamic Memory Allocation
Pointers and Dynamic Variables
Clear1 and Clear2 clear1(int array[], int size) { int i; for (i = 0; i < size; i += 1) array[i] = 0; } clear2(int *array, int size) {
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
Pointers, Dynamic Data, and Reference Types
Dynamic Memory A whole heap of fun….
Dynamic Memory Allocation
Memory Allocation CS 217.
Given the code to the left:
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
Dynamic Memory A whole heap of fun….
Arrays an array of 5 ints is filled with 3,2,4,1,7
Dynamic Memory A whole heap of fun….
Dynamic Memory.
Pointers & Arrays.
Dynamic Memory And Objects
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
The Stack.
COP 3330 Object-oriented Programming in C++
Dynamic Memory CSCE 121.
Pointers, Dynamic Data, and Reference Types
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Dynamic Memory A whole heap of fun…

Review: The Stack C++ allocates variables on a stack All memory that might be used in function allocated at one time Address Identifier Value 116 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100

Review: The Stack void foo(int q) { if(true) { char c = 'a'; } } void bar() { int x = 8; } int main() { int x = 10; int y = 1; foo(6); int z = 5; bar(); } Address Identifier Value 116 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100

Review: The Stack void foo(int q) { if(true) { char c = 'a'; } } void bar() { int x = 8; } int main() { int x = 10; int y = 1; foo(x); int z = 5; bar(); } Address Identifier Value 116 ?? 115 114 113 112 111 z 110 109 108 107 y 106 105 104 103 X 102 101 100

Review: The Stack void foo(int q) { if(true) { char c = 'a'; } } void bar() { int x = 8; } int main() { int x = 10; int y = 1; foo(x); int z = 5; bar(); } Address Identifier Value 116 ?? 115 114 113 112 111 z 110 109 108 107 y 106 105 104 103 X 10 102 101 100

Review: The Stack void foo(int q) { if(true) { char c = 'a'; } } void bar() { int x = 8; } int main() { int x = 10; int y = 1; foo(x); int z = 5; bar(); } Address Identifier Value 116 ?? 115 114 113 112 111 z 110 109 108 107 y 1 106 105 104 103 X 10 102 101 100

Review: The Stack void foo(int q) { if(true) { char c = 'a'; } } void bar() { int x = 8; } int main() { int x = 10; int y = 1; foo(x); int z = 5; bar(); } Address Identifier Value 116 c ?? 115 q 10 114 113 112 111 z 110 109 108 107 y 1 106 105 104 103 X 102 101 100

Review: The Stack void foo(int q) { if(true) { char c = 'a'; } } void bar() { int x = 8; } int main() { int x = 10; int y = 1; foo(x); int z = 5; bar(); } Address Identifier Value 116 c 'a' 115 q 10 114 113 112 111 z ?? 110 109 108 107 y 1 106 105 104 103 X 102 101 100

Review: The Stack void foo(int q) { if(true) { char c = 'a'; } } void bar() { int x = 8; } int main() { int x = 10; int y = 1; foo(x); int z = 5; bar(); } Address Identifier Value 116 'a' 115 10 114 113 112 111 z ?? 110 109 108 107 y 1 106 105 104 103 X 102 101 100

Review: The Stack void foo(int q) { if(true) { char c = 'a'; } } void bar() { int x = 8; } int main() { int x = 10; int y = 1; foo(x); int z = 5; bar(); } Address Identifier Value 116 'a' 115 10 114 113 112 111 z 5 110 109 108 107 y 1 106 105 104 103 X 102 101 100

Review: The Stack void foo(int q) { if(true) { char c = 'a'; } } void bar() { int x = 8; } int main() { int x = 10; int y = 1; foo(x); int z = 5; bar(); } Address Identifier Value 116 'a' 115 x 10 114 113 112 111 z 5 110 109 108 107 y 1 106 105 104 103 X 102 101 100

Review: The Stack void foo(int q) { if(true) { char c = 'a'; } } void bar() { int x = 8; } int main() { int x = 10; int y = 1; foo(x); int z = 5; bar(); } Address Identifier Value 116 'a' 115 x 8 114 113 112 111 z 5 110 109 108 107 y 1 106 105 104 103 X 10 102 101 100

Review: The Stack void foo(int q) { if(true) { char c = 'a'; } } void bar() { int x = 8; } int main() { int x = 10; int y = 1; foo(x); int z = 5; bar(); } Address Identifier Value 116 'a' 115 8 114 113 112 111 z 5 110 109 108 107 y 1 106 105 104 103 X 10 102 101 100

Review: The Stack void foo(int q) { if(true) { char c = 'a'; } } void bar() { int x = 8; } int main() { int x = 10; int y = 1; foo(x); int z = 5; bar(); } Address Identifier Value 116 'a' 115 8 114 113 112 111 5 110 109 108 107 1 106 105 104 103 10 102 101 100

What will this do? getPointerToTen Initialize a variable Makes a pointer to it Returns that pointer Main prints twice

The Stack C++ allocates variables on a stack int* getPointerToTen() { int x = 10; int* px = &x; return px; } int main() { int* pTen = getPointerToTen(); cout << *pTen << endl; } Address Identifier Value 116 115 114 113 112 111 110 109 108 107 106 105 104 103 pTen ?? 102 101 100

The Stack C++ allocates variables on a stack int* getPointerToTen() { int x = 10; int* px = &x; return px; } int main() { int* pTen = getPointerToTen(); cout << *pTen << endl; } Address Identifier Value 116 115 114 113 112 111 px 104 110 109 108 107 x 10 106 105 103 pTen ?? 102 101 100

The Stack C++ allocates variables on a stack int* getPointerToTen() { int x = 10; int* px = &x; return px; } int main() { int* pTen = getPointerToTen(); cout << *pTen << endl; } Address Identifier Value 116 115 114 113 112 111 104 110 109 108 107 10 106 105 103 pTen 102 101 100

??? Pointers to items on stack may go bad

The Stack Traditional model: Stack grows down in memory CODE GLOBALS

The Stack Traditional model: Stack grows down in memory Each function adds a Stack Frame : new set of local variables CODE GLOBALS STACK STACK FRAME

The Stack Traditional model: Stack grows down in memory Each function adds a Stack Frame : new set of local variables Exiting a function removes a stack frame CODE GLOBALS STACK STACK FRAME

The Heap The Heap is the extra space Managed by the OS Aka Free Store C++ functions request parts of heap from OS CODE GLOBALS STACK STACK FRAME HEAP

The Heap Heap is unaffected by changes to stack CODE GLOBALS STACK

The Heap Heap is unaffected by changes to stack CODE GLOBALS STACK HEAP Stays until explicitly freed

Dynamic Allocation Dynamic Allocation : Allocate space on heap Done with new keyword Address Identifier Value 2000 1999 1998 1997 1996 1995 1994 1993 1992 … 1000 999 998 997 996 995

Dynamic Allocation Dynamic Allocation : Allocate space on heap New returns pointer, must store Address Identifier Value 2000 p 1000 1999 1998 1997 1996 1995 1994 1993 1992 … ??? 999 998 997 996 995 Values in heap do not have identifiers… must have pointer to them!

Dynamic Allocation Dynamic Allocation : Allocate space on heap Deference to access Address Identifier Value 2000 p 1000 1999 1998 1997 1996 1995 1994 1993 1992 … 100 999 998 997 996 995

Power of Heap How will this time be different?

The Stack int* getGoodPointerToTen() { int* px = new int(10); return px; } int main() { int* pTen = getPointerToTen(); cout << *pTen << endl; } Address Identifier Value 2000 pTen ??? 1999 1998 1997 1996 px 1000 1995 1994 1993 1992 … 10 999 998 997 996 995

The Stack int* getGoodPointerToTen() { int* px = new int(10); return px; } int main() { int* pTen = getPointerToTen(); cout << *pTen << endl; } Address Identifier Value 2000 pTen 1000 1999 1998 1997 1996 1995 1994 1993 1992 … 10 999 998 997 996 995

Dangers Losing track of memory "memory leak" 2000 myData 1000 1996 Address Identifier Value 2000 myData 1000 1996 1992 1988 1984 1980 1976 1972 1968 … 5 996 992 988 984 980 Losing track of memory "memory leak"

Dangers Losing track of memory "memory leak" Address Identifier Value 2000 myData 996 1996 1992 1988 1984 1980 1976 1972 1968 … 1000 5 8 992 988 984 980 Losing track of memory "memory leak" Asked for two ints, only remember where one is!

Accessing Heap Values delete tells OS we are done with memory 2000 Address Identifier Value 2000 myData 1000 1996 1992 1988 1984 1980 1976 1972 1968 … 5 996 992 988 984 980 delete tells OS we are done with memory

Accessing Heap Values delete tells OS we are done with memory 2000 Address Identifier Value 2000 myData 1000 1996 1992 1988 1984 1980 1976 1972 1968 … 5 996 992 988 984 980 delete tells OS we are done with memory

Accessing Heap Values delete tells OS we are done with memory Address Identifier Value 2000 myData 1996 1992 1988 1984 1980 1976 1972 1968 … 1000 5 996 992 988 984 980 delete tells OS we are done with memory Nulling pointer prevents using that memory

Malloc / Free In C there is no new/delete Malloc allocates given number of bytes Returns untyped pointer - cast to desired type Free releases memory

Compiler Rules Items on stack must be predictable size Why arrays must be constant size

Compiler Rules Items on stack must be predictable size Why arrays must be constant size Items in the heap can be any size at all Arrays in the heap are flexible

Arrays & Pointers Array = memory address of base element Pointer = address of item of data Largely interchangeable: Address Identifier Value 2000 nums[4] 5 1996 nums[3] 4 1992 nums[2] 3 1988 nums[1] 2 1984 nums 1 1980 pToArray 1976 1972 1968 … 1000 996 992 988 984 980

Dynamic Array Array on heap can be variable sized Store result as a pointer Then use that pointer as an array: Address Identifier Value 2000 nums2 984 1996 1992 1988 1984 1980 1976 1972 1968 … 1000 5 996 4 992 3 988 2 1 980

Returning Dynamic Array Returning arrays Can return array as pointer Better be created on heap!

Deleting Arrays Delete with [] to free memory in an array 2000 nums2 Address Identifier Value 2000 nums2 984 1996 1992 1988 1984 1980 1976 1972 1968 … 1000 5 996 4 992 3 988 2 1 980 } int* nums = new int[listSize]; //do stuff... //delete old array - free that memory delete [] nums; //allocate new array and use nums to point to it nums = new int[listSize];

Deleting Arrays Delete with [] to free memory in an array 2000 nums2 Address Identifier Value 2000 nums2 984 1996 1992 1988 1984 1980 1976 1972 1968 … 1000 5 996 4 992 3 988 2 1 980 } int* nums = new int[listSize]; //do stuff... //delete old array - free that memory delete [] nums; //allocate new array and use nums to point to it nums = new int[listSize];

Deleting Arrays Delete with [] to free memory in an array 2000 nums2 Address Identifier Value 2000 nums2 1996 1992 1988 1984 1980 1976 1972 1968 … 1000 5 996 4 992 3 988 2 984 1 980 } int* nums = new int[listSize]; //do stuff... //delete old array - free that memory delete [] nums; //allocate new array and use nums to point to it nums = new int[listSize];

How Do I Use In Project? To read in number and make storage must use dynamic memory: