入门的第一个c程序调试问题

iq199 iq199 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
程序如下: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"
为什么我得到的却是警告信息?

多谢!
...全文

相关推荐

评论 14
收藏 已收藏
分享
返回顶部
iq199
Linux/Unix社区 23,224 成员 74,535 内容