VisualAge C++ Tips
Use the Performance Analyzer
What Is It?
The Performance Analyzer is a marvelous tool for analyzing the behavior and performance of your applications. You create a trace file by running it along with your application, and then you analyze the data by looking at it with the Analyzer's 5 views:
- Call Nesting Diagram
- Dynamic Call Graph
- Execution Density Diagram
- Statistics Diagram
- Time Line Diagram
One of the most interesting is the Dynamic Call Graph:
[Screen capture of Performance Analyzer Call Graph]
The nodes (rectangles) represent functions and the arcs (lines) represent calls.
The size, shape, and color of the nodes gives information about the time spent in each function! Gray, blue, yellow, and red colors are used for functions which use up to 1/8, 1/4, 1/2, and the maximum of the total execution time, respectively. The height of a node depicts the execution time of a function, and the width depicts the active time (the time on the stack, even while a callee is running).
The arcs are similarly colored to represent the frequency of calls.
How Is It Enabled?
If you're using the workframe, enabling the Analyzer is as simple as clicking on the Build Smarts button on the toolbar, and selecting the Analyzer checkbox on the dialog that appears:
[Build Smarts Dialog Box]
Then click on Build to rebuild your application with the the Analyzer enabled. To start the Analyzer, choose "Analize" from the Project menu.
Use the Debug Memory Management Facilities
What are they?
Debugging versions of malloc, calloc, realloc, free, new and delete
What do they offer?
Information about memory you failed to free Notice that you are freeing already freed memory Notice that you wrote over freed memory
How do you enable them?
Use the /Tm VisualAge C++ compiler option
Sample Program
Compile this program with /Tm. Run it with the w command line option (no slash) to write over memory which has already been freed. Notice that it catches the error. Run it again with the f command line option and watch it detect your freeing the same object twice.
Compile it again without /Tm and run it the same two ways. The memory management offenses are undetected.
#include <stdlib.h> #include <string.h> enum DemoMode { WriteOverFreed = 'w', FreeTwice = 'f' }; void main (int argc, char ** argv) { if (argc == 2) { char * psz = new char [20]; strcpy (psz, "String 1"); _dump_allocated (10); switch (argv[1][0]) { case WriteOverFreed: delete [] psz; strcpy (psz, "String 2"); _heap_check (); break; case FreeTwice: delete [] psz; delete [] psz; break; } } }