Debugging Dynamic Memory Usage Errors Using HP WDB
Example 10 High Water-Mark Feature
Sample Program
$cat high.c
1 #include <stdio.h>
2 #include <malloc.h>
3
4 char * func1()
5 {
6 char *ptr;
7 ptr = malloc(1000);
8 return ptr;
9
10 }
11
12 char * func2()
13 {
14 char *ptr;
15 ptr = malloc (100);
16 return ptr;
17
18 }
19
20 void func4()
21 {
22 char *ptr;
23 ptr = malloc (9000);
24 free (ptr);
25
26 }
27
28 void main()
29 {
30 char* ptr;
31 int i;
32
33 for (i=0; i<100; i++)
34 {
35 ptr = func1();
36 free(ptr);
37 }
38 ptr = func2();
39 func4();
40 }
Sample Debugging Session
Case 1: The info heap high-mem command displays the number of times that the break value
changes for a given run and to display the highest break value in the current run.
$ gdb high
HP gdb 5.6 for HP Itanium (32 or 64 bit) and target HP-UX 11.2x.
Copyright 1986 - 2001 Free Software Foundation, Inc.
Hewlett-Packard Wildebeest 5.6 (based on GDB) is covered
by the GNU General Public License.
Type "show copying" to see the conditions to change it and/or
distribute copies. Type "show warranty" for warranty/support.
..
(gdb) b 40 /*set a breakpoint at the end of your application to view
the highest break value for the current run*/
Breakpoint 1 at 0x4000b90:2: file high.c, line 40 from high.
(gdb) set heap-check on
(gdb) r
Starting program: high
44