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

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.xdnf.cn/news/1536093.html

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

相关文章

ElementUI 布局——行与列的灵活运用

ElementUI 布局——行与列的灵活运用 一 . 使用 Layout 组件1.1 注册路由1.2 使用 Layout 组件 二 . 行属性2.1 栅格的间隔2.2 自定义元素标签 三 . 列属性3.1 列的偏移3.2 列的移动 在现代网页设计中&#xff0c;布局是构建用户界面的基石。Element UI 框架通过其强大的 <e…

计算架构模式之接口高可用

接口高可用整体框架 接口高可用主要应对两类问题&#xff1a;雪崩效应和链式效应。 雪崩&#xff1a;当请求量超过系统处理能力之后&#xff0c;会导致系统性能螺旋快速下降&#xff0c;本来系统可以处理1000条&#xff0c;但是当请求量超过1200的时候&#xff0c;此时性能会下…

深入理解算法效率:时间复杂度与空间复杂度

目录 引言 一、算法效率的基础 二、时间复杂度 1.概念 2.常见类型 1.O(1) — 常数阶 2.O(n) — 线性阶 3.O(n^2) — 平方阶 4.O(2^&#x1d45b;) — 指数阶 5.O(log &#x1d45b;) — 对数阶 3.总结 三、空间复杂度 1.概念 2.常见类型 1.O(1) — 常数阶 2.…

为你的 Github 仓库引入自动构建的Github Pages静态页面

1. 设置config文件 在Github仓库根目录下创建_config.yml文件。其中的内容为&#xff1a; plugins:- jekyll-relative-links relative_links:enabled: truecollections: true include:- CONTRIBUTING.md- README.md- LICENSE.md- COPYING.md- CODE_OF_CONDUCT.md- CONTRIBUTI…

性能小白终于能看懂Jmeter报告了

对于刚接触性能测试的初学者来说&#xff0c;分析JMeter生成的测试报告无疑是一个巨大的挑战。面对大量的数据信息&#xff0c;如何快速理解响应时间、吞吐量、错误率等关键指标&#xff0c;往往让人感到困惑。今天&#xff0c;让我们一起探讨如何轻松看懂JMeter的性能测试报告…

沉浸式体验和评测Meta最新超级大语言模型405B

2024年7月23日&#xff0c; 亚马逊云科技的AI模型托管平台Amazon Bedrock正式上线了Meta推出的超级参数量大语言模型 - Llama 3.1模型&#xff0c;小李哥也迫不及待去体验和试用了该模型&#xff0c;那这么多参数量的AI模型究竟强在哪里呢&#xff1f;Llama 3.1模型是Meta&…

nodejs 007:错误npm error Error: EPERM: operation not permitted, symlink

完整错误信息 npm error Error: EPERM: operation not permitted, symlink npm warn cleanup Failed to remove some directories [ npm warn cleanup [ npm warn cleanup C:\\Users\\kingchuxing\\Documents\\IPFS\\orbit-db-set-master\\node_modules\\ipfs-cli, npm…

C++11 回调函数

【C引用进阶】C11 回调函数 文章目录 【C引用进阶】C11 回调函数 回调函数的实现往往是应用层&#xff08;更上层&#xff09;的程序拥有&#xff0c;而调用者是底层的程序。 相当于说&#xff0c;底层的程序是一个服务员&#xff0c;应用层程序是客人&#xff0c;客人需要客房…

天融信把桌面explorer.exe删了,导致开机之后无windows桌面,只能看到鼠标解决方法

win10开机进入桌面&#xff0c;发现桌面无了&#xff0c;但是可以ctrlaltdelete调出任务管理器 用管理员权限打开cmd&#xff0c;输入&#xff1a; sfc /scanfilec:\windowslexplorer.exe 在运行C:\windows\Explorer.exe&#xff1b;可以进入桌面&#xff0c;但是隔离几秒钟…

VMamba: Visual State Space Model 论文总结

题目&#xff1a;VMamba: Visual State Space Model&#xff08;视觉状态空间模型&#xff09; 论文&#xff1a;[2401.10166] VMamba: Visual State Space Model (arxiv.org) 源码&#xff1a;https://arxiv.org/pdf/2401.10166 (github.com) 目录 一、摘要 二、引言 三、方…

【JS|第27期】网页文件传输:Blob与Base64的对决

日期&#xff1a;2024年9月12日 作者&#xff1a;Commas 签名&#xff1a;(ง •_•)ง 积跬步以致千里,积小流以成江海…… 注释&#xff1a;如果您觉得有所帮助&#xff0c;帮忙点个赞&#xff0c;也可以关注我&#xff0c;我们一起成长&#xff1b;如果有不对的地方&#xf…

keil 中 printf重定向

int fputc(int ch, FILE *f) {HAL_UART_Transmit(&huart1, (void*)&ch, 1, 1000);return ch;} 同时勾选&#xff0c;使用微库

1.使用 VSCode 过程中的英语积累 - File 菜单(每一次重点积累 5 个单词)

前言 学习可以不局限于传统的书籍和课堂&#xff0c;各种生活的元素也都可以做为我们的学习对象&#xff0c;本文将利用 VSCode 页面上的各种英文元素来做英语的积累&#xff0c;如此做有 3 大利 这些软件在我们工作中是时时刻刻接触的&#xff0c;借此做英语积累再合适不过&a…

p11 日志,元数据,进程的查看

直接运行docker run -d centos这个时候回启动容器&#xff0c;但是因为容器里面没有前台进程所以这个时候docker会把没用的进程给停止掉&#xff0c;可以看到docker ps命令没有查看到任何的正在运行的容器 但是如果说你使用 -it命令进入到了容器里面&#xff0c;这个他就不会…

9.15javaweb项目总结

1.贴吧界面算是完成了基本的 能通过url打开多个贴吧信息的界面了&#xff0c;界面水平不是很高&#xff0c;界面还有待提升&#xff0c;然后该界面的功能点还差点有点远&#xff0c;完成度不是很高。 2.解决了关注的功能问题 要考虑的地方有点多&#xff0c;最简单的就是点击…

DockerLinux安装DockerDocker基础

Linux软件安装 yum命令安装 通过yum命令安装软件,是直接把软件安装到Linux系统中 安装和卸载都比较麻烦,因为软件和系统是强关联的 Docker docker是一种容器技术,可以解决软件和系统强关联关系,使得软件的安装和卸载更方便,它可以将我们的应用以及依赖进行打包,制作出一个镜…

CTF(misc)1和0的故事

题目链接 下载题目后是一堆整齐的01字符串&#xff0c;猜测是生成二维码&#xff0c;将0变成白色方块&#xff0c;1变成黑色方块。 0000000001110010000000000 0000000000011110100000000 0000000001110001000000000 0000000010111100000000000 0000000010101010000000000 00…

分享一个 在线拍卖系统 商品竞拍平台Java、python、php三个技术版本(源码、调试、LW、开题、PPT)

&#x1f495;&#x1f495;作者&#xff1a;计算机源码社 &#x1f495;&#x1f495;个人简介&#xff1a;本人 八年开发经验&#xff0c;擅长Java、Python、PHP、.NET、Node.js、Android、微信小程序、爬虫、大数据、机器学习等&#xff0c;大家有这一块的问题可以一起交流&…

C++11的部分新特性

目录 1.列表初始化 1.1 { } 初始化 1.2 std::initializer_list 2.声明 2.1 auto 2.2 decltype 2.3 nullptr 3. 范围for 4.STL中的一些变化 5.右值引用与移动语义 5.1 左值引用与右值引用 5.2 左值引用与右值引用的比较 5.3 右值引用使用场景 5.4 完美转发 6.新的…

软件设计师——程序设计语言

目录 低级语言和高级语言 编译程序和解释程序 正规式&#xff0c;词法分析的一个工具 有限自动机 ​编辑 上下文无关法 ​编辑 中后缀表示法 杂题 ​编辑 低级语言和高级语言 编译程序和解释程序 计算机只能理解由0、1序列构成的机器语言&#xff0c;因此高级程序设计…