入门的第一个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"
为什么我得到的却是警告信息?

多谢!
...全文
342 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
lodestar51 2005-05-12
  • 打赏
  • 举报
回复
不懂,学习..
wooin 2005-05-10
  • 打赏
  • 举报
回复
mark
iq199 2005-04-21
  • 打赏
  • 举报
回复
我写了一个最简单的c程序,也出现同样的问题

代码如下:
main()
{
char *str;
int size = 10;
int i;
str = (char*)malloc(size);
for (i = size; i > 1; i--)
str[size - i] = '*';
str[size - 1] = '\0';
printf("str=%s\n", str);
}



编译
gcc -g -o test test.c

调试
gdb test

(gdb) b 10
Breakpoint 1 at 0x80483e0: file test.c, line 10.

(gdb) r
Starting program: /root/tmp/test
Breakpoint 1, main() at test.c:10
10 str[size - i] = '*';

(gdb) watch str[size - i]
Hardware watchpoint 2: str[size - i]

(gdb) n

warning: Could not remove hardware watchpoint 2.
Warning:
Could not insert hardware watchpoint 2.
Could not insert hardware breakpoints:
You may have requested too may hardware breakpoints/watchpoints.


同样在其它版本的linux下,就能正确监视str[size-i]。
在多台fedora core 3上,都有同样的问题。
iq199 2005-04-21
  • 打赏
  • 举报
回复
to gaoxianfeng(高)

这样试验了,问题还是一样


谁手里有fedora core 3,帮忙试验一下,多谢!!!!!!
gaoxianfeng 2005-04-19
  • 打赏
  • 举报
回复
没有接触过 :(
你这样试试看
就是别 b 24
而是b main
n
直到执行到24行
你再watch string2[size - i]
看看如何


gz
iq199 2005-04-18
  • 打赏
  • 举报
回复
(gdb) info watch
Num Type Disp Enb Address What
1 breakpoint keep y 0x0804847c in my_print2 at greeting.c:24
breakpoing already hit 1 time
2 hw watchpoint keep y string2[size - i]


这个能看出问题来吗

刚使了redhat linux 9,也没有问题
又试验了一台fedora core 3,也是同样的问题
gaoxianfeng 2005-04-18
  • 打赏
  • 举报
回复
fedora core 3
未知领域
:(
gaoxianfeng 2005-04-18
  • 打赏
  • 举报
回复
你自己编译生成的gdb?

一般struct breakpoint *b 结构内的inserted被设置成1了才 会报这个错误
而b->address不可设置了 才会有如上操作

如果没有自己make
你info watch
看看是不是 数量超了
iq199 2005-04-18
  • 打赏
  • 举报
回复
奇怪 在一台redhat linux 6.1的机器上,gdb可以正常执行
但是在 fedora core 3里面,却不能正常执行。
用gdb监视简单的值可以,比如size,但是监视string2[size - i]就是不行。
iq199 2005-04-18
  • 打赏
  • 举报
回复
to: gaoxianfeng(高)

这个程序就是一个有问题的程序,网上的教程讲的就是用gdb调试这个程序。
教程地址:http://purec.binghua.com/Article/Class1/Class2/200501/400.html
我现在的问题不是程序的错误,而是为什么gdb里面,不能监视string[size - i]。
为什么会有warning : Could not remove hardware watchpoint 这样的提示
gaoxianfeng 2005-04-18
  • 打赏
  • 举报
回复
把string2[size - i] 改成 string2[size - i - 1]
string2[size+1] = '\0'; 改成 string2[size] = '\0';
gaoxianfeng 2005-04-18
  • 打赏
  • 举报
回复
string2[size+1] = '\0';
先把如上 和 下面的代码 理解一下吧


for (i = 0; i < size; i++)
string2[size - i] = string[i];

从0开始 你知道最后一个string2[size-i] size-i是什么 是1 那么string2[0]是什么? 有可能是'\0' 你的string2字符串就是空

string2[size+1] = '\0'; size+1是什么?
不溢出吗?


iq199 2005-04-18
  • 打赏
  • 举报
回复
又仔细检查了一下程序,没有问题,可执行结果还是不对

[iq199@MyFedora ~]$ cd program/
[iq199@MyFedora program]$ ls
greeting greeting.c
[iq199@MyFedora program]$ gcc -g -o greeting greeting.c
[iq199@MyFedora program]$ gdb greeting
GNU gdb Red Hat Linux (6.1post-1.20040607.41rh)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu"...Using host libthread_db library "/lib/tls/libthread_db.so.1".

(gdb) break 24
Breakpoint 1 at 0x804847c: file greeting.c, line 24.
(gdb) run
Starting program: /home/iq199/program/greeting
The string is hello there

Breakpoint 1, my_print2 (string=0xfee62d00 "hello there") at greeting.c:24
24 string2[size - i] = string[i];
(gdb) watch string2[size - i]
Hardware watchpoint 2: string2[size - i]
(gdb) 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.

(gdb)
alaiyeshi 2005-04-18
  • 打赏
  • 举报
回复
我把你的程序编译运行了,也用gdb跟踪了,没出现你的问题,跟你说的正确结果一致

23,118

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧