- 论坛徽章:
- 0
|
根据网上的一篇文章在学习gdb时遇到了下面的问题,盼高手指教。
程序代码如下:
#include <iostream>;
using namespace std;
void my_print(char *my_string);
void my_print2(char *my_string);
int main(void)
{
char my_string[]="Hello!";
my_print(my_string);
cout<<endl;
my_print2(my_string);
}
void my_print(char *my_string)
{
cout<<"The string in normal secquence is : "
<<my_string;
}
void my_print2(char *my_string)
{
char *string2;
int size,i;
int size2;
size=strlen(my_string);
size2=size-1;
string2=(char *)malloc(size+1);
for(i=0;i<size;i++)
string2[size2-i]=my_string;
string2[size]='\0';
cout<<"The string printed backward is: "
<<string2;
cout<<endl;
}
编译时用的命令: g++ -g Hello.cpp
调试顺序如下:
gdb a.out
run
list
list
list
break 27
提示:
Breakpoint 1, my_print2 (my_string=0xfeff13d0 "Hello!" at Hello.cpp:27
27 string2[size2-i]=my_string;
然后 run
watch string2[size2-i]
提示:Hardware watchpoint 2: string2[size2 - i]
再执行:next
提示:warning: Could not remove hardware watchpoint 2.
Warning:
Could not insert hardware watchpoint 2.
Could not insert hardware breakpoints:
You may have requested too many hardware breakpoints/watchpoints.
而按照文章说的,输出应该有
Old value=
New value=
之类的输出才对。
我自己写的程序就很正常:
#include <iostream>;
using namespace std;
int main()
{
int i=0;
for(int j=0;j<200;j++)
i+=1;
cout<<i;
}
调试如下:
(gdb) break 7
Breakpoint 1 at 0x80486eb: file test.cpp, line 7.
(gdb) run
Starting program: /home/zha/workspace/Hello/a.out
Breakpoint 1, main () at test.cpp:7
7 i+=1;
(gdb) watch i
Hardware watchpoint 2: i
(gdb) next
Hardware watchpoint 2: i
Old value = 0
New value = 1
main () at test.cpp:6
6 for(int j=0;j<200;j++)
(gdb)
哪位能给解释一下,谢谢! |
|