-->
Previous Table of Contents Next


By listing the file, we can see that the place where we want to set the breakpoint is line 24. Now, to set the breakpoint, enter the following command at the gdb command prompt:


(gdb) break 24

gdb should now print a response resembling the following:


Breakpoint 1 at 0x139: file greeting.c, line 24

(gdb)

Now let’s run the program again by typing the run command. This command generates the following output:


Starting program: /root/greeting

The string is hello there



Breakpoint 1, my_print2 (string = 0xbfffdc4 “hello there”) at greeting.c

:24

24 string2[size-i]=string[i]

We can see what is actually going wrong with the program by setting a watch which tells the value of the string2[size - i] variable expression.

To do this, type


(gdb) watch string2[size - i]

gdb returns the following acknowledgment:


Watchpoint 2: string2[size - i]

The version of gdb included on the CD-ROM accompanying this book changes the prompt to Hardware Watchpoint, but this is simply a change in the naming convention used by gdb. Now we can step through the execution of the for loop using the next command:


(gdb) next

After the first time through the loop, gdb tells us that string2[size - i] is ’h’ by displaying the following message on the screen:


Watchpoint 2, string2[size - i]

Old value = 0 ’\000’

New value = 104 ’h’

my_print2(string = 0xbfffdc4 “hello there”) at greeting.c:23

23 for (i=0; i<size; i++)

This is the value that we expected. Stepping through the loop several more times reveals similar results. Everything appears to be functioning normally. When we get to the point where i=10, the value of the string2[size - i] expression is equal to ’e,’ the value of the size - i expression is equal to 1, and the program is at the last character that is to be copied over into the new string.

Step through the loop one more time and it’s clear that there is no value assigned to string2[0], which is the first character of the string. Because the malloc function initializes the memory it assigns to null, the first character in string2 is the null character. This explains why nothing is printed when we tried to print string2.

Now that we’ve found the problem, it should be quite easy to fix. We must write the code so that the first character going into string2 is being put into string2 at offset size - 1 instead of string2 at offset size. This is because the size of string2 is 12, but it starts numbering at offset zero. The characters in the string should start at offset 0 and go to offset 10, with offset 11 being reserved for the null character.

There are many ways to modify this code so that it will work. One way is to keep a separate size variable that is one smaller than the real size of the original string. This solution is shown in the following code:


#include <stdio.h>

main ()

{

  char my_string[] = “hello there”;



  my_print (my_string);

  my_print2 (my_string);

}



my_print (char *string)

{

  printf (“The string is %s\n”, string);

}



my_print2 (char *string)

{

  char *string2;

  int size, size2, i;



  size = strlen (string);

  size2 = size -1;

  string2 = (char *) malloc (size + 1);

  for (i = 0; i < size; i++)

    string2[size2 - i] = string[i];

  string2[size] = ’\0’;

  printf (“The string printed backward is %s\n”, string2);

}

Additional C Programming Tools

The Slackware Linux distribution includes a number of C development tools that have not yet been described. This section describes many of these additional tools and their typical uses.


Previous Table of Contents Next