入门的第一个c程序调试问题
iq199 2005-04-18 02:45:52 程序如下:greeting.c
代码:
#include <stdio.h>
void my_print(char *string);
void my_print2(char *string);
main ()
{
char my_string[] = "hello there";
my_print (my_string);
my_print2 (my_string);
}
void my_print (char *string)
{
printf ("The string is %s\n", string);
}
void my_print2 (char *string)
{
char *string2;
int size, i;
size = strlen (string);
string2 = (char *) malloc (size + 1);
for (i = 0; i < size; i++)
string2[size - i] = string[i];
string2[size+1] = '\0';
printf ("The string printed backward is %s\n", string2);
}
用gcc -g -o greeting greeting.c
正常,执行和网上文章也一样
用gdb greeting,如下
(gdb)break 24就是string2[size - i] = string[i];所在行
Breakpoint 1 at 0x804847c: fine greeting.c, line 24
(gdb)run
Starting program: /home/iq/program/greeting
The string is hello there
Breakpoint 1, my_print2 (string=0xfef6e7e0 "hello there") at greeting.c:24
24 string2[size - i] = string[i]
(gdb)watch string2[size - i]
Hardware watchpoint 2: string[size - i]
(gdb)next
warning : Could not remove hardware watchpoint
Warning :
Could not insert hardware watchpoint 2.
Could not insert hardware breakpoints:
You may have requested too many hardware breakpoints/watchpoints.
按照网上文章,此步骤应会得到string2[size-i]的值,如下:
Watchpoint 2, string2[size - i]
Old value = 0"\000"
New value = 104"h"
为什么我得到的却是警告信息?
多谢!