实验 Linux系统的程序开发工具

实验 Linux系统的程序开发工具

实验内容
1、将下面的参考代码录入到test1.c文件中,编译执行后发现结果与预期不一致,请使用GDB调试,完成字符串反序输出功能。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int reverse_str(char *string);

int main (void) {
char string[] = “Linux C Tools : GCC and GDB”;
printf (“The original string is %s \n”, string);
reverse_str (string);
}

int reverse_str (char *str) {
char *new_str;
int i, size;
size = strlen (str);
if((new_str = (char *) malloc (size + 1)) == NULL) {
return -1;
}

for (i = 0; i < size; i++)
    new_str[size - i] = str[i];
new_str[size+1] = ' ';
printf("The reversed string is %s\n",new_str);
free(new_str);
return 0 ;

}

2、假设我们有一个程序由 5个文件组成,源代码如下:

/main.c/
#include “mytool1.h”
#include “mytool2.h”
int main()
{
mytool1_print(“hello mytool1!”);
mytool2_print(“hello mytool2!”);
return 0;
}
/mytool1.c/
#include “mytool1.h”
#include <stdio.h>
void mytool1_print(char *print_str)
{
printf("This is mytool1 print : %s ",print_str);
}
/mytool1.h/
#ifndef _MYTOOL_1_H
#define _MYTOOL_1_H
void mytool1_print(char *print_str);
#endif
/mytool2.c/
#include “mytool2.h”
#include <stdio.h>
void mytool2_print(char *print_str)
{
printf("This is mytool2 print : %s ",print_str);
}
/mytool2.h/
#ifndef _MYTOOL_2_H
#define _MYTOOL_2_H
void mytool2_print(char *print_str);
#endif
(1)在命令行环境下编译生成可执行文件mytool;
(2)编写基本的makefile文件自动生成mytool;
(3)利用变量控制改进makefile文件;
(4)使用自动推导进一步改进makefile文件;
四、实验过程
1.(1)gedit test1.c 创建test1.c
编入实验代码

(2)编译后结果错误

(3)进入gdb调试
①list 列出代码 设置了两个用于观察的断点

②运行程序,到第一个断点又设置了一个观察点i

③第二次运行到断点,观察i与new_str里的内容

④最后跳出循环后观察new_str里的内容

(4)观察得到,循环开始时就把str[0]赋给了new_str[27],导致new_str数组超限。
所以修改程序第28行语句为new_str[size-i-1] = str[i];

(1)
在命令行环境下编译生成可执行文件mytool;

(2)编写基本的makefile文件自动生成mytool;

(3)利用变量控制改进makefile文件;

(4)使用自动推导进一步改进makefile文件;

完整报告链接:
https://download.csdn.net/download/DQA1505045218/20032492

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值