CS61C 2020计算机组成原理Lab01-数字表示,溢出

news2024/9/22 4:38:20

1. Exercise 1 :See what you can C

# 用gcc 来编译可执行文件如program.c
gcc program.c
# 就可以得到一个executable file named a.out.
./a.out

# 如果想给这个可执行文件命名,则使用 -o
gcc -o program program.c
./program
# 使用-g 能得到一个 可执行程序的debugger程序
gcc -g -o hello hello.c

# 开始dubug,运行,在mac上用lldb
lldb hello

2. Exercise 2:Catch those bugs!

Answer the questions:

  1. While you’re in a gdb session, how do you set the arguments that will be passed to the program when it’s run?
chenhuiyi@bogon lab01 % lldb hello

(lldb) settings set target.run-args 1 2 3
(lldb) run
  1. How do you create a breakpoint?
(lldb) breakpoint set --name main
(lldb) breakpoint set --file test.c --line 12

此时,设置了断点但还没有执行程序,按以下步骤来执行程序并在断点处停止:

(lldb) target create my_program

启动你的程序,可以使用 run 命令:

(lldb) run

当程序执行到你之前设置的断点位置时,LLDB会自动将程序暂停,并在 (lldb) 提示符下等待你的命令,如图所示:

在这里插入图片描述

  1. How do you execute the next line of C code in the program after stopping at a breakpoint?
(lldb) step
  1. if the next line of code is a function call, you’ll execute the whole function call at once if you use your answer to #3. (If not, consider a different command for #3!) How do you tell GDB that you want to debug the code inside the function (i.e. step into the function) instead? (If you changed your answer to #3, then that answer is most likely now applicable here.)

使用 step 命令,但带上 -i(或 --into)选项,它告诉LLDB进入函数内部进行调试。

(lldb) step -i

当你完成函数内部的调试后,你可以使用 step 命令继续执行函数之后的代码,或者使用 finish 命令退出函数调用并返回到调用函数的上下文

5.How do you continue the program after stopping at a breakpoint?

(lldb) continue
  1. How can you print the value of a variable (or even an expression like 1+2) in gdb?
(lldb) print my_variable
(lldb) print 1 + 2

在这里插入图片描述

7.How do you configure gdb so it displays the value of a variable after every step?

可以通过设置检查点:

(lldb) watchpoint set variable my_variable

在这里插入图片描述

或者使用 display(我用display才能在打印台上看到)

(lldb) display count
  1. How do you show a list of all variables and their values in the current function?
(lldb) frame variable

在这里插入图片描述

argc=1表示只有一个变量,这个变量就是这个程序名称本身

  1. How do you quit out of lldb?
(lldb) quit
(lldb) q

Exercise 3:Debugging w/ YOU(ser input)

The purpose of this exercise is to make you unafraid of running the debugger even when your program needs user input. It turns out that you can send text to stdin, the file stream read by the function fgets in this silly program, with some special characters right from the command line.

Hint 1: If you’re creating a text file containing your input, you’re on the right track!

(lldb)settings set target.input-path xxx.txt
(lldb)run

在这里插入图片描述
或者直接使用process launch 则不需要再加上run

(lldb) process launch --stdin input.txt

在这里插入图片描述

如果不进入lldb这个调试器的话,可以直接在terminal向可执行程序输入:

./a.out < fileName.txt

在这里插入图片描述

Exercise 4: Valgrind’ing away

lab推荐的是 Valgrind这个工具,是一个模拟你的GPU并且跟踪内存访问的程序。但是,在macos上没有可用的版本,通过这个命令可以看出:

brew info valgrind

segfault_ex 开始。你应该观察到了一个段错误(segfault),当程序试图访问它无权访问的内存时就会发生崩溃

在这里插入图片描述

在macos上,可以使用Instruments 来检测内存访问问题:

(未解决)

nts 来检测内存访问问题:

(未解决)

Exercise 5:Pointers and Structures in C

Here’s one to help you in your interviews. In ll_cycle.c, complete the function ll_has_cycle() to implement the following algorithm for checking if a singly- linked list has a cycle.

  1. Start with two pointers at the head of the list. We’ll call the first one tortoise and the second one hare.
  2. Advance hare by two nodes. If this is not possible because of a null pointer, we have found the end of the list, and therefore the list is acyclic.
  3. Advance tortoise by one node. (A null pointer check is unnecessary. Why?)
  4. If tortoise and hare point to the same node, the list is cyclic. Otherwise, go back to step 2.

If you want to see the definition of the node struct, open the ll_cycle.h header file.

ll_cycle.h:

#ifndef LL_CYCLE_H
#define LL_CYCLE_H
typedef struct node {
    int value;
    struct node *next;
} node;

int ll_has_cycle(node *);
#endif

ll_cycle.c补充代码:

#include <stddef.h>
#include "ll_cycle.h"

int ll_has_cycle(node *head) {
    /* your code here */
    // 判断一个链表是否有环,通常称为“乌龟和兔子”算法(也被称为Floyd的循环检测算法)。
    //这个算法的核心思想是使用两个指针(乌龟和兔子)以不同的速度遍历链表。如果链表中存在环,这两个指针最终会在环内相遇。
    node *tortoise = head,*hare = head;
    while(hare!=NULL && hare->next!=NULL){ // 确保hare可以移动两步
        hare = hare->next->next;
        tortoise = tortoise->next;
        if(tortoise == hare){
            return 1;
        }
    }
    return 0;
}
#选项 -c 表示只编译和汇编,但不链接。它会生成一个名为 ll_cycle.o 的目标文件(object file)
$ gcc -c ll_cycle.c
$ gcc -c test_ll_cycle.c
# 此命令将之前生成的两个目标文件(test_ll_cycle.o 和 ll_cycle.o)链接在一起,
# 创建一个可执行文件。-o test_ll_cycle 指定了输出的可执行文件名为 test_ll_cycle。
$ gcc -o test_ll_cycle test_ll_cycle.o ll_cycle.o
$ ./test_ll_cycle

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2139095.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

微服务保护之熔断降级

在微服务架构中&#xff0c;服务之间的调用是通过网络进行的&#xff0c;网络的不确定性和依赖服务的不可控性&#xff0c;可能导致某个服务出现异常或性能问题&#xff0c;进而引发整个系统的故障&#xff0c;这被称为 微服务雪崩。为了防止这种情况发生&#xff0c;常用的一些…

K8s 之控制器的定义及详细调用案例

什么是控制器 官方文档&#xff1a; https://v1-30.docs.kubernetes.io/zh-cn/docs/concepts/workloads/controllers/ 控制器也是管理pod的一种手段 自主式pod&#xff1a;pod退出或意外关闭后不会被重新创建控制器管理的 Pod&#xff1a;在控制器的生命周期里&#xff0c;始…

《深度学习》PyTorch 手写数字识别 案例解析及实现 <下>

目录 一、回顾神经网络框架 1、单层神经网络 2、多层神经网络 二、手写数字识别 1、续接上节课代码&#xff0c;如下所示 2、建立神经网络模型 输出结果&#xff1a; 3、设置训练集 4、设置测试集 5、创建损失函数、优化器 参数解析&#xff1a; 1&#xff09;para…

Cesium 计算3d凸包(ConvexHull)

Cesium 计算3d凸包(ConvexHull) Cesium 计算3d凸包(ConvexHull)

Unity实战案例全解析:PVZ 植物放置分析

前篇&#xff1a;Unity实战案例全解析&#xff1a;PVZ 植物卡片状态分析-CSDN博客 植物应该如何从卡牌状态转为实物&#xff1f; 其实就只需要考虑两个步骤加一个后续处理&#xff1a; 1.点击卡牌后就实例化 需要一个植物状态枚举&#xff0c;因为卡牌分为拿在手上和种植下…

Android 10.0 mtk平板camera2横屏预览旋转90度横屏保存圆形预览缩略图旋转90度功能实现

1.前言 在10.0的系统rom定制化开发中,在进行一些平板等默认横屏的设备开发的过程中,需要在进入camera2的 时候,默认预览图像也是需要横屏显示的,在上一篇已经实现了横屏预览功能,然后发现横屏预览后,点击录像和照片下保存的圆形预览缩略图 依然是竖屏的,所以说同样需要…

需求导向的正则表达式

目录 re.sub 需求&#xff1a;把 1. 2.这些序号转成&#xff08;1&#xff09; &#xff08;2&#xff09; 需求&#xff1a;反过来&#xff0c;把(1)->1. ,&#xff08;2&#xff09;》2. 。 需求&#xff1a;把出现的 1 2 3都转成下标 进阶1&#xff01;只想让化学符…

Redis入门2

在java中操作Redis Redis的Java客户端 Redis 的 Java 客户端很多&#xff0c;常用的几种: Jedis Lettuce Spring Data Redis Spring Data Redis 是 Spring 的一部分&#xff0c;对 Redis 底层开发包进行了高度封装。 在 Spring 项目中&#xff0c;可以使用Spring Data R…

把项目部署到Linux系统上(如何在阿里云服务器上安装和配置SpringBoot+vue全栈开发环境)

项目部署上线 环境准备下载安装Linux系统和ssh连接工具背景知识安装虚拟机安装Linux系统选择installCentOS7按命令IP addr查看服务器IP地址&#xff0c;ens33网卡中会出现IP地址配置好后就可以查看了一个可远程连接Linux服务器的工具1.&#xff08;基于finalshell工具&#xff…

小明震惊OpenAI 的新模型 01

在硅谷的中心&#xff0c;繁忙的咖啡馆和创业中心周围&#xff0c;年轻的软件工程师小明坐在他的办公桌前&#xff0c;面露困惑。科技界一直在盛传一项新的AI突破&#xff0c;但他持怀疑态度&#xff0c;不敢抱太大希望。他认为AI泡沫即将破灭&#xff0c;炒作列车即将出轨&…

网络原理 IP协议与以太网协议

博主主页: 码农派大星. 数据结构专栏:Java数据结构 数据库专栏:MySQL数据库 JavaEE专栏:JavaEE 关注博主带你了解更多数据结构知识 目录 1.网络层 IP协议 1.IP协议格式 2.地址管理 2.1 IP地址 2.2 解决IP地址不够用的问题 2.3NAT网络地址转换 2.4网段划分 3.路由选择…

北极星计划的回响:从Leap Motion到Midjourney的AI 3D硬件梦想

在科技的浩瀚星空中,总有一些梦想如同北极星般璀璨,指引着探索者前行。六年前,Leap Motion的CEO David以一篇充满激情的博客文章,向我们揭示了“北极星计划”——一个旨在打破数字与物理界限,创造流畅统一体验的增强现实平台。今天,随着Midjourney在AI文生图领域的全球爆…

2024.9.15周报

一、题目信息 题目&#xff1a;Physics-informed neural networks for solving flow problems modeled by the 2D Shallow Water Equations without labeled data 链接&#xff1a;物理信息神经网络用于解决由二维浅水方程建模的流动问题&#xff0c;无需标记数据- ScienceDi…

【Node.js】初识 RabbitMQ

概述 MQ 顾名思义&#xff0c;是消息队列。 RabbitMQ 是一个消息队列系统&#xff0c;用于实现异步通信。基于 AMQP。AMQP(高级消息队列协议) 实现了对于消息的排序&#xff0c;点对点通讯&#xff0c;和发布订阅&#xff0c;保持可靠性、保证安全性。 在 Node.js 的微服务架…

LAMP+WordPress

一、简介 LAMP&#xff1a; L&#xff1a;linux——操作系统&#xff0c;提供服务器运行的基础环境。A&#xff1a;apache&#xff08;httpd&#xff09;——网页服务器软件&#xff0c;负责处理HTTP请求和提供网页内容。M&#xff1a;mysql&#xff0c;mariadb——数据库管理…

PCL 窗口可视化两个点云

目录 一、概述 1.1原理 1.2实现步骤 1.3 应用场景 二、代码实现 2.1关键函数 2.2完整代码 三、实现效果 PCL点云算法汇总及实战案例汇总的目录地址链接&#xff1a; PCL点云算法与项目实战案例汇总&#xff08;长期更新&#xff09; 一、概述 本文将介绍如何使用PCL库…

8.4Prewitt算子边缘检测

基本原理 Prewitt算子是一种用于边缘检测的经典算子&#xff0c;它通过计算图像中像素值的&#xff08;一阶导数&#xff09;梯度来检测边缘。Prewitt算子通常包括两个3x3的卷积核&#xff0c;一个用于检测水平方向上的边缘&#xff0c;另一个用于检测垂直方向上的边缘。 示例…

【动漫资源管理系统】Java SpringBoot助力,搭建一个高清动漫在线观看网站

&#x1f34a;作者&#xff1a;计算机毕设匠心工作室 &#x1f34a;简介&#xff1a;毕业后就一直专业从事计算机软件程序开发&#xff0c;至今也有8年工作经验。擅长Java、Python、微信小程序、安卓、大数据、PHP、.NET|C#、Golang等。 擅长&#xff1a;按照需求定制化开发项目…

【插件】【干货】用EPPlus在Unity中读写Excel表

EPPlus是什么我就不说了&#xff0c;你都点进来了肯定知道 几个常用的api 1.index下标都是从1开始的 2.可以读取任意单元格上的任意内容&#xff0c;不需要给excel表写规则 但是如果你写了规则&#xff0c;就需要自己用额外的代码 --- 数据结构去实现 3.打开excel表 ExcelP…

[数据集][目标检测]智慧交通铁路异物入侵检测数据集VOC+YOLO格式802张7类别

数据集格式&#xff1a;Pascal VOC格式YOLO格式(不包含分割路径的txt文件&#xff0c;仅仅包含jpg图片以及对应的VOC格式xml文件和yolo格式txt文件) 图片数量(jpg文件个数)&#xff1a;802 标注数量(xml文件个数)&#xff1a;802 标注数量(txt文件个数)&#xff1a;802 标注类别…