Debugging Dynamic Memory Usage Errors Using HP WDB (5900-1474; WDB 6.2; January 2011)

14 printf("No memory available, aborting...\n");
15 abort ();
16 }
17
18 return memBlock;
19 }
20
21 /* Create a header string and return to the caller */
22 char *createHeader()
23 {
24 char *headerString = (char *) allocateMemory(20);
25 strcpy (headerString, "Header : My string ");
26 return headerString;
27 }
28
29 /* Create a string with header and input. Return to the caller */
30 char *createString(char *input, int n)
31 {
32 #define MAXBADINPUT (10)
33
34 char *hdrString, *myString;
35 int inputLength = strlen(input);
36 static int badInput;
37 hdrString = createHeader();
38 /* Input length too small ? return back from here */
39 if (n < inputLength + 20)
40 {
41 hdrString = NULL;
42 printf("Too small size for a string, not allocating\n");
43 badInput++;
44 printf("Number of badInput is %d\n", badInput);
45 if (badInput > MAXBADINPUT)
46 {
47 printf("Too many bad inputs.. aborting\n");
48 abort();
49 }
50 return NULL;
51 }
52
53 /* Allocate the string, copy the header to it */
54 myString = (char *)allocateMemory(n);
55 strcpy(myString, hdrString);
56 free(hdrString);
57
58 /* Copy the input to the string after header and return */
59 strcpy(myString+20, input);
60 return (myString);
61 }
62
63
64 void compareStrings()
65 {
66 int s1length, s2length;
67 char *str1 = NULL, *str2 = NULL;
68
69 /* Allocate first string and initialize it */
70 do
90