繁体   English   中英

gcc -O分段错误

[英]gcc -O segmentation fault

这是将数据插入二进制特里的代码。 如果我使用基本的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选项的情况下仍能正常工作。 我真的很想让它工作,如果你们中的一些人可以帮助我理解,那就太好了。

谢谢 !

没有简短的自包含示例 ,就不可能知道某些问题是什么

但是,有一些调试方法可以帮助您发现问题所在。 在代码中添加几个断言

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,断言中的所有代码都将被忽略。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM