Presentation is loading. Please wait.

Presentation is loading. Please wait.

understanding memory usage by a c++ program

Similar presentations


Presentation on theme: "understanding memory usage by a c++ program"— Presentation transcript:

1 understanding memory usage by a c++ program

2 code memory static memory stack memory heap memory

3 Every variable/object has a scope
Where in the program code is the name of a variable/object visible? Depends on where the name was declared Global scope - throughout the program Local scope – within a function Scope is known at compile time

4 Every variable/object has a lifetime
Lifetime is a run-time issue The period of time during program execution that a variable/object is making use of memory space Global variables use memory space from start to end of program execution A function’s local variables use memory space only while that function is executing Amount of memory needed for a function’s local variables is known at compile time When a function Is called space for its variables is stored on the stack When a function returns that space is released

5 Memory usage by Lab1 program
main findAverage displayResult getNumber doubleLargest

6 code memory static memory stack memory heap memory program code cin
cout activation records for function(s) currently executing

7 What is stored in the activation record for a function?
Value of each local variable (parameters are local variables) Address of code to be executed when function returns

8 code memory static memory stack memory heap memory program code cin
cout activation records for function(s) currently executing main num num average

9 code memory static memory stack memory heap memory program code cin
cout activation records for function(s) currently executing main getNum num num average num

10 code memory static memory stack memory heap memory program code cin
cout activation records for function(s) currently executing main num num average

11 code memory static memory stack memory heap memory program code cin
cout activation records for function(s) currently executing main getNum num num average num

12 code memory static memory stack memory heap memory program code cin
cout activation records for function(s) currently executing main num num average

13 code memory static memory stack memory heap memory program code cin
cout activation records for function(s) currently executing main double Larger num num average num num2

14 code memory static memory stack memory heap memory program code cin
cout activation records for function(s) currently executing main num num average

15 code memory static memory stack memory heap memory program code cin
cout activation records for function(s) currently executing main display num num average average

16 code memory static memory stack memory heap memory program code cin
cout activation records for function(s) currently executing main num num average

17 code memory static memory stack memory heap memory program code cin
cout activation records for function(s) currently executing

18 New and heap memory the c++ new statement requests memory for an object at run-time When a new statement is executed Requested space is allocated in the heap Address of the heap space is returned Suppose a function contains the following code Int n1 ; n1 = 36; Int* n2 = new int; *n2 = 36; N1 and n2 are local variables whose space is stored on the stack The space for *n2 is in the heap What happens when this function is called?

19 Activation record created on the stack
code memory static memory stack memory heap memory main function n1 n2

20 When new is executed code memory static memory stack memory
heap memory main function n1 n2

21 When function returns? code memory static memory stack memory
heap memory main function n1 n2

22 When function returns code memory static memory stack memory
heap memory main

23 What happens when the following function is called?
func caller stack void func (){ ArrayList aList; LinkedList lList; aList.append(“C++”); lList.append(“Java”); } heap

24 func caller func stack heap aList items size head size lList

25 lList.append(“Java”);
func caller func stack heap aList items size head size lList aList.append(“C++”); lList.append(“Java”);

26 What happens when func returns to its caller?
func caller func stack heap aList items size 1 head size lList aStack.push(“C++”); lStack.push(“Java”); Java What happens when func returns to its caller?

27 func caller stack heap Java

28 A program that uses new to allocate heap memory space has to
Make sure all memory allocated using new is released (using delete) when it is no longer needed Which linkedlist methods use new? Which linkedlist methods need to use delete?

29 The big 3 Compiler provides 3 methods for all types (including classes you define) A destructor Frees memory resources used by an object of that type Called when an object goes “out of scope” Ex: function to which it is local returns A copy constructor Makes a copy of an existing object Value parameter requires a copy of the argument Return myobject; requires a copy of myobject Assignment operator (operator=) Replaces one existing object with a copy of another existing object Ex: vec2 = vec1; Ex: mylist2 = mylist1;

30 Some classes need to override the big 3 methods provided by the compiler
Classes that do not use heap memory space can use the compiler provided methods Fraction Arraylist (a fixed size array does not use heap memory) Vectorlist (a vector does use heap memory but has the big 3 methods already defined) Classes that use heap memory space must override the big 3 to Prevent memory leaks when an object goes out of scope Make a “deep” copy of an object rather than a “shallow” copy

31 copy constructor makes a copy of an existing object when needed
Compiler provided copy constructor makes a copy of an existing object by making a copy of all of the data members This results in a shallow copy rather than a deep copy

32 a shallow copy of a LinkedList
original copy size head size head 3 3 3 copy.append(----);

33 after copy.append(----)
original copy size head size head 4 3 3 ----

34 a deep copy of a LinkedList
original copy size head size head 3 3

35 Prototypes for the big 3 ~classname(); // returns heap memory space used by this object Classname (const classname & object_to_copy); // makes a deep copy of object_to_copy Classname& operator=(const classname & object_to_copy); // returns heap memory space being used by object being assigned to // and replaces it with a deep copy of object_to_copy Last line will be return *this (allows assignments to be chained)

36 Try this Write a function that is sent the pointer to the beginning of a linked list and uses delete to return the nodes of that linked list one at a time Void destroy_list(Node* p); size head 3

37 Try this Write a function that is sent the pointer to the beginning of a linked list and makes a deep copy of that linked list Void copy_list(Node* p); size head 3


Download ppt "understanding memory usage by a c++ program"

Similar presentations


Ads by Google