为什么我在C中的以下代码中遇到了段错误?

如何解决为什么我在C中的以下代码中遇到了段错误?

| 我在操作系统类中有一个项目,该项目应该模拟转换后备缓冲区。 我正在写一个将在TLB丢失后调用的方法。它应该在TLB中找到下一个为空或不久没有被击中的条目,删除该条目,并将其替换为上次调用的页表中的条目。当调用该方法时,将给出页表条目中的数据。
Void tlb_insert(VPAGE_NUMBER new_vpage,PAGEFRAME_NUMBER new_pframe,BOOL new_mbit,BOOL new_rbit)
{
  // Starting at the clock_hand\'th entry,find first entry to
  // evict with either valid bit  = 0 or the R bit = 0. If there
  // is no such entry,then just evict the entry pointed to by
  // the clock hand.

  int m;
  int evct = clock_hand;
  for (m = clock_hand; m < (num_tlb_entries); m++){
    if (tlb[m].vbit_and_vpage & VBIT_MASK == 0 || tlb[m].mr_pframe & RBIT_MASK == 0){
      evct = m;

      break;
    }
  }

  // Then,if the entry to evict has a valid bit = 1,// write the M and R bits of the of entry back to the M and R
  // bitmaps,respectively,in the MMU (see mmu_modify_rbit_bitmap,etc.
  // in mmu.h)

  if (tlb[evct].vbit_and_vpage & VBIT_MASK == 1){
    PAGEFRAME_NUMBER pfr = tlb[evct].mr_pframe & PFRAME_MASK;
    int val1 = tlb[evct].mr_pframe & RBIT_MASK;
    int val2 = tlb[evct].mr_pframe & MBIT_MASK;
    mmu_modify_rbit_bitmap (pfr,val1);
    mmu_modify_mbit_bitmap(pfr,val2);
  }

  // Then,insert the new vpage,pageframe,M bit,and R bit into the
  // TLB entry that was just found (and possibly evicted).
   tlb[evct].vbit_and_vpage = VBIT_MASK | new_vpage;
   tlb[evct].mr_pframe = new_mbit | (new_rbit | new_pframe); 
  // Finally,set clock_hand to point to the next entry after the
  // entry found above.
  clock_hand = evct + 1;
}

//Writes the M  & R bits in the each valid TLB
//entry back to the M & R MMU bitmaps.
 void tlb_write_back()
{
   int n;
  for (n = 0; n < num_tlb_entries; n++){
    if (tlb[n].vbit_and_vpage & VBIT_MASK == 1){
       PAGEFRAME_NUMBER pfr = tlb[n].mr_pframe & PFRAME_MASK;
       int val1 = tlb[n].mr_pframe & RBIT_MASK;
       int val2 = tlb[n].mr_pframe & MBIT_MASK;
       mmu_modify_rbit_bitmap (pfr,val1);
       mmu_modify_mbit_bitmap(pfr,val2); 
    }   
    } 
  } 
我正在从段中得到段错误:
tlb[evct].vbit_and_vpage = VBIT_MASK | new_vpage;
tlb[evct].mr_pframe = new_mbit | (new_rbit | new_pframe); 
VBIT_MASK是先前定义的变量,用于掩盖我现在要插入的位。我不确定我是否误解了如何使用位掩码,或者我的代码是否存在更严重的错误。我意识到,要求任何人详细介绍整个过程实在太多了,但是如果有人对我应该解决的方向有任何建议,我将不胜感激!     

解决方法

我提请您注意这张表中的
&
低得令人惊讶:
$ cat /usr/share/misc/operator
Operator                                        Associativity
-------------------------------------------------------------
() [] -> .                                      left to right
! ~ ++ -- - (type) * & sizeof new delete        right to left
->* .*                                          left to right
* / %                                           left to right
+ -                                             left to right
<< >>                                           left to right
< <= > >=                                       left to right
== !=                                           left to right
&                                               left to right
^                                               left to right
|                                               left to right
&&                                              left to right
||                                              left to right
?:                                              right to left
= += -= *= /= %= <<= >>= &= ^= |= throw         right to left
?: (C++,third operand)                         right to left,left to right

$FreeBSD: src/share/misc/operator,v 1.2.22.1 2009/05/31 18:14:24 ed Exp $
    ,假设使用正确的工具,则非常容易发现段错误。通常我只是从
gdb
开始,查看回溯并立即知道原因。因此,我没有给您遍历代码(我没有),而是给您提供了查找任何段错误(以及许多其他错误)的一般方法: 如果在Linux系统上使用GCC,建议您使用with5ѭ(重新)编译代码。
-Wall
将显示有趣的警告,这通常是导致未定义行为或段错误的原因,
-g -ggdb
将一些有用的调试信息添加到您的代码中,ѭ8optimization禁用优化(这样就不会优化循环中的计数器变量,依此类推)。 之后,您应使用
gdb ./yourprog
启动调试器。然后写
run
启动程序。程序崩溃后,您将看到类似“ \“ segfault,程序以... \'退出。键入
bt
,它显示回溯(即,包括行号等功能调用的堆栈。)。在列表中并搜索程序中第一个最顶层的文件。这样,您现在将知道发生段故障的确切位置(文件和行号),并且通常非常容易决定发生什么错误。原因是,如果您知道确切的行(请考虑一下该语句中可能被统一化或为NULL的内容)。 或者,您也可以在该行设置
breakpoint yourfile.c:123
(在本示例中为行号123),并用
print your_var_or_pointer
显示变量的内容。检查该行中的所有变量-现在您终于应该知道是什么原因了:D (PS:我不能给您建议如何在其他环境(例如Visual Studio等)中进行调试,但是想法是相同的。它们都附带了一个出色的调试器!)     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-