为什么我在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等)中进行调试,但是想法是相同的。它们都附带了一个出色的调试器!)     

要回复问题请先登录注册