Overview of Previous Lesson(s) Over View 3  Debugger  A computer program that is used to test and debug other programs.  Local Debugging  Debugging.

Slides:



Advertisements
Similar presentations
Chapter 18 Vectors and Arrays
Advertisements

1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs.
Pointers.
Chapter 18 Vectors and Arrays John Keyser’s Modification of Slides by Bjarne Stroustrup
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
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.
R4 Dynamically loading processes. Overview R4 is closely related to R3, much of what you have written for R3 applies to R4 In R3, we executed procedures.
Module R2 CS450. Next Week R1 is due next Friday ▫Bring manuals in a binder - make sure to have a cover page with group number, module, and date. You.
1 CSC241: Object Oriented Programming Lecture No 28.
Overview of Previous Lesson(s) Over View  Assertions  assert() library function is declared in the cassert header to check logical conditions in native.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Linked Lists Chained nodes of information create what are called linked lists, with each node providing a link to the next node. A useful feature of linked.
CS 225 Lab #2 - Pointers, Copy Constructors, Destructors, and DDD.
1 Class Constructors a class constructor is a member function whose purpose is to initialize the private data members of a class object the name of a constructor.
Review of C++ Programming Part II Sheng-Fang Huang.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
Stack and Heap Memory Stack resident variables include:
Technical Module : Pointers #1 2000/01Scientific Computing in OOCourse code 3C59 Technical Module : Pointers In this module we will cover Pointers to primitives.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Pointers OVERVIEW.
1 Chapter 15-2 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
CSCI-383 Object-Oriented Programming & Design Lecture 18.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Chapter 9 Classes: A Deeper Look, Part I Part II.
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 3 – Inventory Application: Introducing Variables,
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
1 // SPECIFICATION FILE (dynarray.h) // Safe integer array class allows run-time specification // of size, prevents indexes from going out of bounds, //
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Learners Support Publications Constructors and Destructors.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
Pointers and References. Pointers & Memory 0x000x040x080x0B0x100x140x180x1B0x20.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Yan Shi CS/SE2630 Lecture Notes
Memory Management.
Constructors and Destructors
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Dynamic Storage Allocation
Pointers and Dynamic Arrays
CMSC 341 Lecture 2 – Dynamic Memory and Pointers (Review)
Learning Objectives Pointers as dada members
Lesson One – Creating a thread
Programming with ANSI C ++
LESSON 20.
Class Operations Pointer and References with class types
C Basics.
Memberwise Assignment / Initialization
Array Lists Chapter 6 Section 6.1 to 6.3
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 15 Pointers, Dynamic Data, and Reference Types
Constructors and Destructors
9-10 Classes: A Deeper Look.
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
More C++ Classes Systems Programming.
9-10 Classes: A Deeper Look.
SPL – PS3 C++ Classes.
Presentation transcript:

Overview of Previous Lesson(s)

Over View 3  Debugger  A computer program that is used to test and debug other programs.  Local Debugging  Debugging process and the source code are deployed on the same machine.  Remote Debugging  Source program is running on a separate machine and the debugging takes place on an isolated box

Over View.. 4  Dry Run  A dry run is sometimes defines as a mental run of a computer program.  Where the computer programmer examines the source code one step at a time and determines what it will do when run.

Contents  Debugging Dynamic Memory  Checking the Free Store  Controlling Free Store  Free Store Debugging Output  Memory Leaks 6

Debugging Dynamic Memory  Allocating memory dynamically is a potent source of bugs, and perhaps the most common bugs in this context are memory leaks.  A memory leak arises when we use the new operator to allocate memory.  We never use the delete operator to free it again. 7

Debugging Dynamic Memory..  Memory leaks present no obvious symptoms much of the time, but memory leaks are detrimental to the performance of your machine because memory is being occupied to no good purpose.  Sometimes, it can result in a catastrophic failure of the program when all available memory has been allocated. 8

Debugging Dynamic Memory…  For checking program’s use of the free store, Visual C++ provides a range of diagnostic routines.  These routines use a special debug version of the free store.  These are declared in the header crtdbg.h.  All calls to these routines are automatically removed from the release version of your program. 9

Checking the Free Store  A concise overview of techniques involved  In checking free store operations.  How memory leaks can be detected.  The functions declared in crtdbg.h check the free store using a record of its status stored in a structure of type CrtMemState.  Lets check this struct.. 10

Checking the Free Store.. typedef struct _CrtMemState { struct _CrtMemBlockHeader* pBlockHeader; // Ptr to most recently allocated block unsigned long lCounts[_MAX_BLOCKS]; // Counter for each type of block unsigned long lSizes[_MAX_BLOCKS]; // Total bytes allocated in each block type unsigned long lHighWaterCount; // The most bytes allocated at a time up to now unsigned long lTotalCount; // The total bytes allocated at present } _CrtMemState; 11

Checking the Free Store…  Not too many functions involved in tracking free store operations.  Capabilities :  To record the state of the free store at any point  To determine the difference between two states of the free store  To output state information  To output information about objects in the free store  To detect memory leaks 12

Checking the Free Store… void _CrtMemCheckpoint(_CrtMemState* state);  This stores the current state of the free store in a CrtMemState structure.  The argument pass to the function is a pointer to a CrtMemState structure in which the state is to be recorded. 13

Checking the Free Store… int _CrtMemDifference(_CrtMemState* stateDiff, const _CrtMemState* oldState, const _CrtMemState* newState);  This function compares the state specified by the third argument, with a previous state that is specified in the second argument.  The difference is stored in a CrtMemState structure specified in the first argument.  If the states are different, the function returns a non - zero value (true) otherwise, 0 (fals ) is returned. 14

Checking the Free Store… void _CrtMemDumpStatistics(const _CrtMemState* state);  This dumps information about the free store state specified by the argument to an output stream.  The state structure pointed to by the argument can be a state that you recorded using CrtMemCheckpoint() or the difference between two states produced by CrtMemDifference(). 15

Checking the Free Store… void _CrtMemDumpAllObjectsSince(const _CrtMemState* state);  It dumps information on objects allocated in the free store.  Since the state of the free store is specified by the argument, this has been recorded by an earlier call in program to CrtMemCheckpoint() 16

Checking the Free Store… int _CrtDumpMemoryLeaks();  It checks for memory leaks and dumps information on any leak that is detected.  By enabling this mechanism we get automatic detection of any memory leaks that occurred during program execution 17

Controlling Free Store  We can control free store debug operations by setting a flag, crtDbgFlag, which is of type int.  This flag incorporates five separate control bits, including one to enable automatic memory leak checking.  Control bits can be specified by using the following identifiers : 18

Controlling Free Store 19

Controlling Free Store  By default, the CRTDBG_ALLOC_MEM_DF bit is on, and all the others are off.  To set the crtDbgFlag flag you pass a flag of type int to the CrtDbgFlag() function that implements the combination of indicators.  This puts flag into effect and returns the previous status of CrtDbgFlag. 20

Controlling Free Store  One way to set the indicators is to first obtain the current status of the crtDbgFlag flag. int flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); // Get current flag  We can set or unset the indicators by combining the identifiers for the individual indicators with this flag using bitwise operators. 21

Controlling Free Store  To set an indicator on, OR the indicator identifier with the flag.  To set the automatic leak checking indicator on, in the flag: flag |= _CRTDBG_LEAK_CHECK_DF; 22

Controlling Free Store  To turn an indicator off, AND the negation of the identifier with the flag.  To turn off tracking of memory that is used internally by the library, you could write: flag & = ~_CRTDBG_CHECK_CRT_DF; 23

Controlling Free Store  To put a new flag into effect, simply make a call to CrtSetDbgFlag() with flag as the argument: _CrtSetDbgFlag(flag);  Alternatively, we can OR all the identifiers for the indicators that we want together, and pass the result as an argument.  If you just want to leak check when the program exits _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_M EM_DF); 24

Free Store Debugging  The destination of the output from the free store debugging functions is not the standard output stream.  By default it goes to the debug message window.  To see the output on stdout, There are two functions involved. 25

Free Store Debugging  CrtSetReportMode()  which sets the general destination for output  CrtSetReportFile()  which specifies a stream destination. 26

Free Store Debugging  The CrtSetReportMode() function is declared as int _CrtSetReportMode(int reportType, int reportMode)  There are three kinds of output produced by the free store debugging functions.  Each call to the CrtSetReportMode() function sets the destination specified by the second argument for the output type specified by the first argument. 27

Free Store Debugging  We can specify the report type by one of the following identifiers 28

Free Store Debugging  We can specify the report mode by one or combination of the following identifiers 29

Memory Leak Detection  Lets check how can we detect the memory leaks … 30

Problems  The objects reported as being left in the free store are presented with the most recently allocated first, and the earliest last.  It is obvious from the output that the Name class is allocating memory for its data members, and never releasing it.  The problem our class has is that we forgot the fundamental rules relating to classes. 31

Solution  Always define  A destructor  A copy constructor  The assignment operator.  Lets back to editor to see how our class would be… 32

Thank You 33