c++ memory model dynamicmemory heap vs stack · deleting data on the heap: delete to freememory on...

22
C++ MEMORY MODEL DYNAMIC MEMORY HEAP VS STACK Problem Solving with Computers-I Hi Freq Af

Upload: others

Post on 07-Jun-2020

13 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: C++ MEMORY MODEL DYNAMICMEMORY HEAP VS STACK · Deleting data on the Heap: delete To freememory on the heap use the deleteoperator p int p new int C2 delete P Hfrees the heap memory

C++ MEMORY MODELDYNAMIC MEMORYHEAP VS STACK

Problem Solving with Computers-I

Hi Freq Af

Page 2: C++ MEMORY MODEL DYNAMICMEMORY HEAP VS STACK · Deleting data on the Heap: delete To freememory on the heap use the deleteoperator p int p new int C2 delete P Hfrees the heap memory

AnnouncementsPlease fill out the mid-quarter evaluations for:

(1) TAs: http://bit.ly/CS16-Midquarter-TA-Evaluation

(2) Course and instructor:http://bit.ly/CS16-Midquarter-Instructor-and-Course-Evaluation

Link to both is posted on Piazza!

Page 3: C++ MEMORY MODEL DYNAMICMEMORY HEAP VS STACK · Deleting data on the Heap: delete To freememory on the heap use the deleteoperator p int p new int C2 delete P Hfrees the heap memory

The case of the disappearing data!int getInt(){

int x=5;return x;

}int* getAddressOfInt(){

int x=10;return &x;

}int main(){

int y=0, *p=nullptr, z=0;y = getInt();p = getAddressOfInt();z = *p;

cout<<y<<", "<<z<<", "<<*p<<endl;}

What is the output?

A. 5, 0, 10B. 5, 10, 10C. Something else

0 2

NDAa 0 o TG P

o

5 10 To

Page 4: C++ MEMORY MODEL DYNAMICMEMORY HEAP VS STACK · Deleting data on the Heap: delete To freememory on the heap use the deleteoperator p int p new int C2 delete P Hfrees the heap memory

C++ Memory Model: Stack• Stack: Segment of memory

managed automatically using a Last in First Out (LIFO) principle

• Think of it like a stack of books!

b g g l

dooUfExecutable

Globeroiofoo.fr nSaio

Jbar3

y bare

17 EEO

Page 5: C++ MEMORY MODEL DYNAMICMEMORY HEAP VS STACK · Deleting data on the Heap: delete To freememory on the heap use the deleteoperator p int p new int C2 delete P Hfrees the heap memory

C++ Memory Model: Heap• Heap: Segment of memory

managed by the programmer

• Data created on the heap stays there

– FOREVER or

– until the programmer explicitly deletes it

s

Managed bythe programnot automatic

Page 6: C++ MEMORY MODEL DYNAMICMEMORY HEAP VS STACK · Deleting data on the Heap: delete To freememory on the heap use the deleteoperator p int p new int C2 delete P Hfrees the heap memory

Creating data on the Heap: new

To allocate memory on the heap use the new operator

int x n is on the stack

in f p's Ubyles

fwin

Daganp 24 same as

fJpnewiP

Page 7: C++ MEMORY MODEL DYNAMICMEMORY HEAP VS STACK · Deleting data on the Heap: delete To freememory on the heap use the deleteoperator p int p new int C2 delete P Hfrees the heap memory

Deleting data on the Heap: delete

To free memory on the heap use the delete operator

int p new int C2

delete P Hfrees theheapmemory

11 p is still pointingto 2

fsiaeg.LIp 5 11Program will crash

11Cannot dereference panymore

Page 8: C++ MEMORY MODEL DYNAMICMEMORY HEAP VS STACK · Deleting data on the Heap: delete To freememory on the heap use the deleteoperator p int p new int C2 delete P Hfrees the heap memory

void tool Sy

int A p Dfw int

delete p

stack 1 Heap

PD1 delete p

Page 9: C++ MEMORY MODEL DYNAMICMEMORY HEAP VS STACK · Deleting data on the Heap: delete To freememory on the heap use the deleteoperator p int p new int C2 delete P Hfrees the heap memory

Dynamic memory management = Managing data on the heap

int* p= new int; //creates a new integer on the heap

Student* n = new Student;

//creates a new Student on the heap

delete p; //Frees the integer

delete n; //Frees the Student

delete n's crash double free error

Page 10: C++ MEMORY MODEL DYNAMICMEMORY HEAP VS STACK · Deleting data on the Heap: delete To freememory on the heap use the deleteoperator p int p new int C2 delete P Hfrees the heap memory

Solve the case of the disappearing data!int getInt(){

int x=5;return x;

}int* getAddressOfInt(){

int x=10;return &x;

}int main(){

int y=0, *p=nullptr, z=0;y = getInt();p = getAddressOfInt();z = *p;cout<<y<<", "<<z<<", "<<*p<<endl;

}

Change the code so that *p does not disappear

Desired output:5, 10, 10

Page 11: C++ MEMORY MODEL DYNAMICMEMORY HEAP VS STACK · Deleting data on the Heap: delete To freememory on the heap use the deleteoperator p int p new int C2 delete P Hfrees the heap memory

Heap vs. stack1 #include <iostream>2 using namespace std;3 4 int* createAnIntArray(int len){5 6 int arr[len];7 return arr;8 9 }

Does the above function correctly return an array of integers?A. YesB. No

Stadeo 1

arrgDl

int a p Create An Int Array 2 p

Page 12: C++ MEMORY MODEL DYNAMICMEMORY HEAP VS STACK · Deleting data on the Heap: delete To freememory on the heap use the deleteoperator p int p new int C2 delete P Hfrees the heap memory

Where are we going? Data Structures!

Arrays

15 20 30

Page 13: C++ MEMORY MODEL DYNAMICMEMORY HEAP VS STACK · Deleting data on the Heap: delete To freememory on the heap use the deleteoperator p int p new int C2 delete P Hfrees the heap memory

arrayint ar 53

Page 14: C++ MEMORY MODEL DYNAMICMEMORY HEAP VS STACK · Deleting data on the Heap: delete To freememory on the heap use the deleteoperator p int p new int C2 delete P Hfrees the heap memory

Where are we going? Data structures!!

(CS24/32)

15 20 30 Arrays

It all boils down to 1’s and 0’s

Page 15: C++ MEMORY MODEL DYNAMICMEMORY HEAP VS STACK · Deleting data on the Heap: delete To freememory on the heap use the deleteoperator p int p new int C2 delete P Hfrees the heap memory

Linked Lists13

Linked List

Array List1 2 3

Page 16: C++ MEMORY MODEL DYNAMICMEMORY HEAP VS STACK · Deleting data on the Heap: delete To freememory on the heap use the deleteoperator p int p new int C2 delete P Hfrees the heap memory

Accessing elements of a linked list

Assume the linked list has already been created, what do the following expressions evaluate to?1. head->data2. head->next->data3. head->next->next->data4. head->next->next->next->data

A. 1B. 2C. 3D. NULLE. Run time error

head

struct Node {int data;Node *next;

};

Page 17: C++ MEMORY MODEL DYNAMICMEMORY HEAP VS STACK · Deleting data on the Heap: delete To freememory on the heap use the deleteoperator p int p new int C2 delete P Hfrees the heap memory

Create a small list – use only the stack15

• Define an empty list

• Add a node to the list with data = 10

struct Node {int data;Node *next;

};

Page 18: C++ MEMORY MODEL DYNAMICMEMORY HEAP VS STACK · Deleting data on the Heap: delete To freememory on the heap use the deleteoperator p int p new int C2 delete P Hfrees the heap memory

Heap vs. stack

Node* createSmallLinkedList(int x, int y){

Node* head = NULL;Node n1 ={x, NULL};Node n2 ={y, NULL};head = &n1;n1->next = &n2;return head;

}

Does the above function correctly create a two-node linked list?A. YesB. No

Page 19: C++ MEMORY MODEL DYNAMICMEMORY HEAP VS STACK · Deleting data on the Heap: delete To freememory on the heap use the deleteoperator p int p new int C2 delete P Hfrees the heap memory

Pointer pitfalls and memory errors• Segmentation faults: Program crashes because it attempted to

access a memory location that either doesn’t exist or doesn’t have permission to access

• Examples of code that results in undefined behavior and potential segmentation fault

int arr[] = {50, 60, 70};

for(int i=0; i<=3; i++){cout<<arr[i]<<endl;

}

int x = 10;int* p;cout<<*p<<endl;

Page 20: C++ MEMORY MODEL DYNAMICMEMORY HEAP VS STACK · Deleting data on the Heap: delete To freememory on the heap use the deleteoperator p int p new int C2 delete P Hfrees the heap memory

Dynamic memory pitfalls

Dangling pointer: Pointer points to a memory location that no longer exists

int* f1(int num){ int* mem1 =new int[num];return(mem1);

}

A. f1B. f2C. BothD. Neither

int* f2(int num){int mem2[num];return(mem2);

}

Which of the following functions returns a dangling pointer?

Page 21: C++ MEMORY MODEL DYNAMICMEMORY HEAP VS STACK · Deleting data on the Heap: delete To freememory on the heap use the deleteoperator p int p new int C2 delete P Hfrees the heap memory

Dynamic memory pitfalls

Example

void foo(){int* p = new int;

}

Memory leaks (tardy free):Heap memory not deallocated before the end of program Heap memory that can no longer be accessed

Page 22: C++ MEMORY MODEL DYNAMICMEMORY HEAP VS STACK · Deleting data on the Heap: delete To freememory on the heap use the deleteoperator p int p new int C2 delete P Hfrees the heap memory

Next time• More Linked Lists