understanding memory usage by a c++ program

Slides:



Advertisements
Similar presentations
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Advertisements

Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Introduction to Programming Lecture 39. Copy Constructor.
Dynamically Allocated Arrays May 2, Quiz 5 Today.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Run-Time Storage Organization
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
CS 225 Lab #2 - Pointers, Copy Constructors, Destructors, and DDD.
Operator Overloading and Memory Management. Objectives At the conclusion of this lesson, students should be able to: Overload C++ operators Explain why.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Runtime Environments Compiler Construction Chapter 7.
ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly.
Pointers Pointer a data type stores a memory address points to whatever the memory location contains A pointer is a variable that can store a memory address.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
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.
1 Writing a Good Program 8. Elementary Data Structure.
1 Inside the Vector Class: with additional needed methods CPS212CPS212 Gordon College.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Dynamically Allocated Arrays December 4, Skip the Rest of this PowerPoint.
Object-Oriented Programming in C++
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
More C++ Features True object initialisation
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
Engineering Classes. Objectives At the conclusion of this lesson, students should be able to: Explain why it is important to correctly manage dynamically.
POINTERS AND MEMORY ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
On dynamic memory and classes ● We previously had only discussed dynamic memory in regards to structs and dynamic arrays. ● However, they can be used (to.
Memory Management.
Yan Shi CS/SE 2630 Lecture Notes
Pointers and Dynamic Arrays
Learning Objectives Pointers as dada members
Chapter 9: Pointers.
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
C++ Object-Oriented Programming
Pointers Revisited What is variable address, name, value?
CSCE 210 Data Structures and Algorithms
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
Dynamically Allocated Memory
CSC 253 Lecture 8.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Array Lists Chapter 6 Section 6.1 to 6.3
Object Oriented Programming COP3330 / CGS5409
CSC 253 Lecture 8.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 9: Pointers.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers And Memory Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
Destruction and Copying
Indirection.
Dynamic Memory A whole heap of fun….
Linked Lists Chapter 4.
Destructor CSCE 121 J. Michael Moore.
Destructor CSCE 121.
Passing Arguments and The Big 5
Dynamic Memory A whole heap of fun….
Dynamic Memory.
Destruction and Copying
Recitation Course 0603 Speaker: Liu Yu-Jiun.
COP 3330 Object-oriented Programming in C++
Passing Arguments and The Big 5
COP 3330 Object-oriented Programming in C++
Pointers and References
Rule of Three Part 1 & 2.
Run-time environments
SPL – PS3 C++ Classes.
Presentation transcript:

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 ----- ----- -----