c - gcc -O 段错误

标签 c gcc memory segmentation-fault binary-tree

这是在二进制树中插入数据的代码。如果我用基本的 gcc main.c -o main 编译这段代码,它就可以完美运行。

/**
     * Insert a new gateway in the tree, at the position corresponding to the
     * subnet address.
     *
     * addr    : Subnet address
     * netmask : Subnet mask
     * gw      : gateway identifier
     * 
     * return  : void.
     */
    void insertMyAlgo(unsigned int addr, unsigned int netmask, unsigned int gw)
    {
       struct node* noeud;
       int i;
       int maskBit = countMaskBit(netmask);

       // Going down in the tree until next mask bit = 0.
       noeud = arbre;
       for (i = 31; i > 31 - maskBit; i--)
       {
          // Bit = 1, go down in the right child.
          if ((addr >> i) & 0x1)
          {
             if (noeud->fd == NULL)
                noeud->fd = allocNode();
             noeud = noeud->fd;
          }

          // Bit = 0, go down in the left child.
          else
          {
             if (noeud->fg == NULL)
                noeud->fg = allocNode();
             noeud = noeud->fg;
          }
       }

       // Insert the gateway in the node corresponding to our subnet address.
       noeud->gateway = gw;
    }

我想使用 -O 选项来优化查找树、查找特定键所花费的时间。当我使用这个 -O 选项执行我的 main 时,我遇到了段错误。 Gdb 给了我以下信息:

Program received signal SIGSEGV, Segmentation fault. insertMyAlgo
(addr=12288, netmask=<optimized out>, gw=3238068734)
    at mainbinaireBench.c:125 125            if (noeud->fg == NULL)
(gdb) print noeud->fg Cannot access memory at address 0x8

所以错误似乎在这里:

 // Bit = 0, go down in the left child.
 else
 {
    if (noeud->fg == NULL)
       noeud->fg = allocNode();
    noeud = noeud->fg;
 }

我真的不知道为什么会出现这个错误,也不知道为什么程序在没有这个 -O 选项的情况下也能运行。我真的很想让它起作用,如果你们中的一些人可以帮助我理解,那就太好了。

谢谢!

最佳答案

如果没有 Short Self Contained Example 就不可能知道什么是错误的

但是有一些调试技巧可以帮助您找到问题所在。 添加几个asserts到你的代码:

void insertMyAlgo(unsigned int addr, unsigned int netmask, unsigned int gw)
{
   struct node* noeud;
   int i;
   int maskBit = countMaskBit(netmask);

   // Going down in the tree until next mask bit = 0.
   assert( arbre!=NULL );
   noeud = arbre;
   for (i = 31; i > 31 - maskBit; i--)
   {
      // Bit = 1, go down in the right child.
      if ((addr >> i) & 0x1)
      {
         if (noeud->fd == NULL)
         {
            noeud->fd = allocNode();
            assert( noeud->fd!=NULL );
            assert( noeud->fd->fd==NULL );
            assert( noeud->fd->fg==NULL );
         }
         noeud = noeud->fd;
      }

      // Bit = 0, go down in the left child.
      else
      {
         if (noeud->fg == NULL)
         {
            noeud->fg = allocNode();
            assert( noeud->fg!=NULL );
            assert( noeud->fg->fd==NULL );
            assert( noeud->fg->fg==NULL );
         }
         noeud = noeud->fg;
      }
   }

   // Insert the gateway in the node corresponding to our subnet address.
   noeud->gateway = gw;
}

如果这些断言中的任何一个失败,您现在可以更好地了解发生了什么。如果没有失败,则说明您减少了搜索空间,问题出在其他地方。
完成调试后,您甚至可以将断言留在那里。只需为发布版本定义 NDEBUG,断言中的所有代码都将被省略。

关于c - gcc -O 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32736392/

相关文章:

c - 无论是否进行调试都无法从 Visual Studio 运行程序?

c - 未解析的外部符号 _glGenBuffers 错误

c - 我如何在控制台上处理 c 中的箭头键?

c - gcc2.9到5.3不同的汇编指令

c - 如何在c中打印 float 的二进制值

c++ - 为什么 read() 比 getc() 慢

c - 检测明显的字节序问题

ubuntu - 尝试在 Ubuntu 上使用 gcc 构建胖通用二进制文件时出错

pointers - Webkit 中的 0xbbadbeef 有什么用?

optimization - 关于内存,每个程序员都应该知道什么?