入门的第一个c程序调试问题
程序如下: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"
为什么我得到的却是警告信息?
多谢!
代码:
#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"
为什么我得到的却是警告信息?
多谢!
...全文
相关推荐
c语言入门与提高
《C语言入门与提高》这一课程旨在为初学者和进阶者提供全面深入的C语言学习资源,覆盖从基础知识到高级技巧的各个层面。
Linux操作系统下C语言编程入门.pdf
### Linux操作系统下C语言编程入门知识点详解#### 一、基础知识概述《Linux操作系统下C语言编程入门》是一本专为初学者设计的学习指南,旨在帮助读者掌握在Linux环境下使用C语言进行编程的基础知识和技术
c语言程序调试方法整理
C语言程序调试是程序员开发过程中不可或缺的一环,它涉及到对代码的查错和修复。本文档详述了C语言程序调试的基本方法和步骤,旨在帮助开发者提高代码质量。首先,人工检查或静态检查是调试的第一步,非常重要
Visual C++程序调试方法入门
"Visual C++程序调试方法入门"本文将为读者介绍Visual C++程序调试方法的入门知识,涵盖调试信息的设置、断点的设置、数据断点、消息断点等多方面的知识点。
C语言从入门到精通 Visual C++ 6.0 环境搭建与程序调试
《C语言从入门到精通》是一本面向初学者的指南,特别关注在Visual C++ 6.0环境下进行程序开发和调试。


