understanding memory usage by a c++ program
code memory static memory stack memory heap memory
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
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
Memory usage by Lab1 program main findAverage displayResult getNumber doubleLargest
code memory static memory stack memory heap memory program code cin cout activation records for function(s) currently executing
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
code memory static memory stack memory heap memory program code cin cout activation records for function(s) currently executing main num1 num2 average
code memory static memory stack memory heap memory program code cin cout activation records for function(s) currently executing main getNum num1 num2 average num
code memory static memory stack memory heap memory program code cin cout activation records for function(s) currently executing main num1 num2 average
code memory static memory stack memory heap memory program code cin cout activation records for function(s) currently executing main getNum num1 num2 average num
code memory static memory stack memory heap memory program code cin cout activation records for function(s) currently executing main num1 num2 average
code memory static memory stack memory heap memory program code cin cout activation records for function(s) currently executing main double Larger num1 num2 average num1 num2
code memory static memory stack memory heap memory program code cin cout activation records for function(s) currently executing main num1 num2 average
code memory static memory stack memory heap memory program code cin cout activation records for function(s) currently executing main display num1 num2 average average
code memory static memory stack memory heap memory program code cin cout activation records for function(s) currently executing main num1 num2 average
code memory static memory stack memory heap memory program code cin cout activation records for function(s) currently executing
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?
Activation record created on the stack code memory static memory stack memory heap memory main function n1 n2
When new is executed code memory static memory stack memory heap memory main function n1 n2
When function returns? code memory static memory stack memory heap memory main function n1 n2
When function returns code memory static memory stack memory heap memory main
What happens when the following function is called? func caller stack void func (){ ArrayList aList; LinkedList lList; aList.append(“C++”); lList.append(“Java”); } heap
func caller func stack heap aList items size head size lList
lList.append(“Java”); func caller func stack heap aList items size head size lList aList.append(“C++”); lList.append(“Java”);
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?
func caller stack heap Java
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?
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;
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
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
a shallow copy of a LinkedList original copy size head size head 3 3 3 copy.append(----); ----- ----- -----
after copy.append(----) original copy size head size head 4 3 3 ----- ----- ----- ----
a deep copy of a LinkedList original copy size head size head 3 3 ----- ----- ----- ----- ----- -----
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)
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 ----- ----- -----
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 ----- ----- -----