dynamic memory

41
Dynamic Memory A whole heap of fun…

Upload: shelby

Post on 11-Jan-2016

143 views

Category:

Documents


6 download

DESCRIPTION

Dynamic Memory. A whole heap of fun…. Review: The Stack. C++ allocates variables on a stack void foo( int q) { if(true) { char c = 'a'; } } int main() { int x = 10; double y = 1.2; foo(5); int z = 5; }. The Stack. C++ allocates variables on a stack - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Dynamic Memory

Dynamic Memory A whole heap of fun…

Page 2: Dynamic Memory

Review: The Stack

• C++ allocates variables on a stackvoid foo(int q) {

if(true) { char c = 'a'; }}

int main() { int x = 10; double y = 1.2; foo(5); int z = 5;}

Address Identifier Value116115114113112111110109108107106105104103102101100

Page 3: Dynamic Memory

The Stack

• C++ allocates variables on a stackvoid foo(int q) {

if(true) { char c = 'a'; }}

int main() { int x = 10; double y = 1.2; foo(5); int z = 5;}

Address Identifier Value116115114113112111110109108107106105104103

X 10102101100

Page 4: Dynamic Memory

The Stack

• C++ allocates variables on a stackvoid foo(int q) {

if(true) { char c = 'a'; }}

int main() { int x = 10; double y = 1.2; foo(5); int z = 5;}

Address Identifier Value116115114113112111

Y 1.2

110109108107106105104103

X 10102101100

Page 5: Dynamic Memory

The Stack

• C++ allocates variables on a stackvoid foo(int q) {

if(true) { char c = 'a'; }}

int main() { int x = 10; double y = 1.2; foo(5); int z = 5;}

Address Identifier Value116115

q 5114113112111

Y 1.2

110109108107106105104103

X 10102101100

Page 6: Dynamic Memory

The Stack

• C++ allocates variables on a stackvoid foo(int q) {

if(true) { char c = 'a'; }}

int main() { int x = 10; double y = 1.2; foo(5); int z = 5;}

Address Identifier Value116 c a115

q 5114113112111

Y 1.2

110109108107106105104103

X 10102101100

Page 7: Dynamic Memory

The Stack

• C++ allocates variables on a stackvoid foo(int q) {

if(true) { char c = 'a'; }}

int main() { int x = 10; double y = 1.2; foo(5); int z = 5;}

Address Identifier Value116115

q 5114113112111

Y 1.2

110109108107106105104103

X 10102101100

Page 8: Dynamic Memory

The Stack

• C++ allocates variables on a stackvoid foo(int q) {

if(true) { char c = 'a'; }}

int main() { int x = 10; double y = 1.2; foo(5); int z = 5;}

Address Identifier Value116115114113112111

Y 1.2

110109108107106105104103

X 10102101100

Page 9: Dynamic Memory

The Stack

• C++ allocates variables on a stackvoid foo(int q) {

if(true) { char c = 'a'; }}

int main() { int x = 10; double y = 1.2; foo(5); int z = 5;}

Address Identifier Value116115

z 5114113112111

Y 1.2

110109108107106105104103

X 10102101100

Page 10: Dynamic Memory

The Stack

• C++ allocates variables on a stackvoid foo(int q) {

if(true) { char c = 'a'; }}

int main() { int x = 10; double y = 1.2; foo(5); int z = 5;}

Address Identifier Value116115114113112111110109108107106105104103102101100

Page 11: Dynamic Memory

What will this do?

• getPointerToTen – Initialize a variable– Makes a pointer to it– Returns that pointer– Main prints twice

Page 12: Dynamic Memory

The Stack

• C++ allocates variables on a stackint* getPointerToTen() {

int x = 10; int* px = &x; return px;}

int main() { int* pTen = getPointerToTen(); cout << *pTen << endl;}

Address Identifier Value116115114113112111

px 104110109108107

x 10106105104103

pTen ??102101100

Page 13: Dynamic Memory

The Stack

• C++ allocates variables on a stackint* getPointerToTen() {

int x = 10; int* px = &x; return px;}

int main() { int* pTen = getPointerToTen(); cout << *pTen << endl;}

Address Identifier Value116115114113112111

104110109108107

10106105104103

pTen 104102101100

Page 14: Dynamic Memory

???

• Pointers to items on stack may go bad

Page 15: Dynamic Memory

The Stack

• Traditional model:– Stack grows down in memory CODE

GLOBALS

STACK

Page 16: Dynamic Memory

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

STACK FRAME

Page 17: Dynamic Memory

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

Page 18: Dynamic Memory

The Heap

• The Heap is the extra space– Aka Free Store

• Managed by the OS – C++ functions request parts of

heap from OS

CODE

GLOBALS

STACK

STACK FRAME

HEAP

HEAP

Page 19: Dynamic Memory

The Heap

• Heap is unaffected by changes to stackCODE

GLOBALS

STACK

STACK

HEAP

HEAP

Page 20: Dynamic Memory

The Heap

• Heap is unaffected by changes to stackCODE

GLOBALS

STACK

HEAP

HEAPStays until explicitly freed

Page 21: Dynamic Memory

Dynamic Allocation

• Dynamic Allocation : Allocate space on heap

• Done with new keyword

Address Identifier Value200019991998199719961995199419931992

… … …1000

999998997996995

Page 22: Dynamic Memory

Dynamic Allocation

• Dynamic Allocation : Allocate space on heap

• New returns pointer, must store

Address Identifier Value2000 p 100019991998199719961995199419931992

… … …1000

???999998997996995

Values in heap do not have identifiers… must have pointer to them!

Page 23: Dynamic Memory

Dynamic Allocation

• Dynamic Allocation : Allocate space on heap

• Deference to access

Address Identifier Value2000 p 100019991998199719961995199419931992

… … …1000

100999998997996995

Page 24: Dynamic Memory

Power of Heap

• How will this time be different?

Page 25: Dynamic Memory

The Stack

int* getGoodPointerToTen() { int* px = new int(10); return px;}

int main() { int* pTen = getPointerToTen(); cout << *pTen << endl;}

Address Identifier Value2000

pTen ???1999199819971996

px 10001995199419931992

… … …1000

10999998997996995

Page 26: Dynamic Memory

The Stack

int* getGoodPointerToTen() { int* px = new int(10); return px;}

int main() { int* pTen = getPointerToTen(); cout << *pTen << endl;}

Address Identifier Value2000

pTen 10001999199819971996

10001995199419931992

… … …1000

10999998997996995

Page 27: Dynamic Memory

Dangers

• Losing track of memory "memory leak"

Address Identifier Value2000 myData 100019961992198819841980197619721968

… … …1000 5996992988984980…

Page 28: Dynamic Memory

Dangers

• Losing track of memory "memory leak"

• Asked for two ints, only rememberwhere one is!

Address Identifier Value2000 myData 99619961992198819841980197619721968

… … …1000 5996 8992988984980…

Page 29: Dynamic Memory

Accessing Heap Values

• delete tells OS we are donewith memory

Address Identifier Value2000 myData 100019961992198819841980197619721968

… … …1000 5996992988984980…

Page 30: Dynamic Memory

Accessing Heap Values

• delete tells OS we are donewith memory

Address Identifier Value2000 myData 100019961992198819841980197619721968

… … …1000 5996992988984980…

Page 31: Dynamic Memory

Accessing Heap Values

• delete tells OS we are donewith memory

• Nulling pointer prevents usingthat memory

Address Identifier Value2000 myData 019961992198819841980197619721968

… … …1000 5996992988984980…

Page 32: Dynamic 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

Page 33: Dynamic Memory

Compiler Rules

• Items on stack must be predictable size– Why arrays must be constant size

Page 34: Dynamic Memory

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

Page 35: Dynamic Memory

Arrays & Pointers

• Array = memory address of base element

• Pointer = address of item of data

• Largely interchangeable:

Address Identifier Value2000 nums[4] 51996 nums[3] 41992 nums[2] 31988 nums[1] 21984 nums 11980 pToArray 1984197619721968

… … …1000996992988984980…

Page 36: Dynamic Memory

Dynamic Array

• Array on heap can be variable sized– Store result as a pointer

– Then use that pointer as an array:

Address Identifier Value2000 nums2 98419961992198819841980197619721968

… … …1000 5996 4992 3988 2984 1980…

Page 37: Dynamic Memory

Returning Dynamic Array

• Returning arrays– Can return array

as pointer– Better be created

on heap!

Page 38: Dynamic Memory

Deleting Arrays

• Delete with [] to free memoryin an array

Address Identifier Value2000 nums2 98419961992198819841980197619721968

… … …1000 5996 4992 3988 2984 1980…

Page 39: Dynamic Memory

Deleting Arrays

• Delete with [] to free memoryin an array

Address Identifier Value2000 nums2 98419961992198819841980197619721968

… … …1000 5996 4992 3988 2984 1980…

Page 40: Dynamic Memory

Deleting Arrays

• Delete with [] to free memoryin an array

Address Identifier Value2000 nums2 019961992198819841980197619721968

… … …1000 5996 4992 3988 2984 1980…

Page 41: Dynamic Memory

How Do I Use In Project?

• To read in number and make storage must use dynamic memory: