以下是一个程序中的一段释放内存的代码,释放对象是一个数据链,采用递归法释放:  
void  CChessDoc::delresult(SResult  *delresult  )  
{  
           if(  delresult  ==  0  )  return  ;  
           if(delresult->down_chain  ==  0  )    
           {  
                       delete  delresult  ;  
                       delresult  =  0  ;  
           }  
           else    
           {  
                       delchain(delresult->down_chain  );  
                       delresult->down_chain  =  0  ;  
           }  
           delete  delresult  ;  
           delresult  =  0  ;  
}//  end  delresult()  
 
//////////////////////////////////////////////////////////////////////////  
void  CChessDoc::delchain(SChainZi  *delchain)  
{              
           if(  delchain  ==  0  )  return    ;  
           SChainZi  *  cha  ;  
           while  (  delchain->Next  )  
           {  
                       if(  delchain->zi  !=  0)  
                             //  考虑到数据链中zi值虽为空,但数据链不断的情况  
                       {  
                                   if(  delchain->zi->down_chain  == 0 )  
                                   {  
                                               delete  delchain->zi  ;  
                                               delchain->zi  =  0  ;  
                                   }  
                                   else  
                                   {  
                                               delresult(delchain->zi  )  ;  
                                               delchain->zi  =  0  ;  
                                   }  
                       }  
                       cha  =  delchain  ;  
                       delchain  =  delchain->Next  ;                          
                       delete  cha    ;            //  从前逐个删除数据链  
                       cha  =  0  ;  
           }  
           delete  delchain  ;  
           delchain  =  0  ;  
}//  end  delchain()  
数据链结构如下:  
struct  SResult              
{  
           short  int  x  ;                                      
           short  int  y  ;  
                 ....  
 
           struct  SResult  *  up_zi ;            //  父分值  
           struct  SChainZi *  down_chain  ;      //  子分值队列  
};  
/////////////////////////////  
//  分值队列链结构体  
struct  SChainZi    
{  
           struct  SResult      *  zi            ;  
           struct  SChainZi     *  Next          ;  
}  ;  
 
奇怪的是,以上函数‘作用不稳定’,几百M的数据链释放后内存依旧不变,也就是释放后程序占用的内存并没有减少。但有时,也能释放,似乎是在数据量少的时候。  
这个问题困扰我很长时间了,苦于得不到‘C++专家’的指导。所以上CSDN请教专家。是不是用DELETE释放内存还有什么特殊的地方?  
 

解决方案 »

  1.   

    数据链结构太复杂,建议更改如下:  
    struct  SResult              
    {  
               short  int  x  ;                                      
               short  int  y  ;  
                     ....  
     
               struct  SResult  *  father;            //  父分值  
               struct  SResult *  next;      //  子分值队列  
    };  
      

  2.   

    首先,判断指针为空最好不要使用==0,进行判断。
    其次,void  CChessDoc::delresult(SResult  *delresult  )  
    {  
               if(  delresult  ==  0  )  return  ;  
               if(delresult->down_chain  ==  0  )    
               {  
                           delete  delresult  ;  //与这个。
                           delresult  =  0  ;  
               }  
               else    
               {  
                           delchain(delresult->down_chain  );  
                           delresult->down_chain  =  0  ;  
               }  
               delete  delresult  ;  //这两句重复
               delresult  =  0  ;    //
    }//  end  delresult()  
      

  3.   

    wuxuan(真心英雄) is right
      

  4.   

    to : wuxuan(真心英雄) ;dadafeng(大大风)
    首先谢谢你们以及楼上各位的回答和UP
    void  CChessDoc::delresult(SResult  *delresult  )  
    {  
               if(  delresult  ==  0  )  return  ;  
               if(delresult->down_chain  ==  0  )    
               {  
                           delete  delresult  ;  
                           delresult  =  0  ;  
               }  
               else    
               {  
                           delchain(delresult->down_chain  );  
                           delresult->down_chain  =  0  ;  
                           delete delresult  ; // 按逻辑因该放在这儿
                           delresult = 0 ;
               }  
               //delete  delresult  ;  //该句放在这儿 主要还是调试时为了应付可能无法预料的特殊情况,为的是函数作用安全的考虑。
               //delresult  =  0  ;  
    }//  end  delresult()  
    其次,程序中判断指针为空用==0 是出于程序的约定(程序中空指针一律赋0)。不过判断指针用==0 的确不妥。
    说到最后似乎各位还没有帮我回答上面的问题哦!^_^
      

  5.   

    第二个函数的while循环出问题了吧 cha  =  delchain  ;  
     delchain  =  delchain->Next  ;                          
     delete  cha   //这两句去掉,在while后面delete试试 
     cha  =  0  ;  //
      

  6.   

    假设布尔变量名字为flag,它与零值比较的标准if 语句如下:
    if (flag) // 表示flag 为真
    if (!flag) // 表示flag 为假假设整型变量的名字为value,它与零值比较的标准if 语句如下:
    if (value == 0)
    if (value != 0)假设浮点变量的名字为x,应当将其转化为
    if ((x>=-EPSINON) && (x<=EPSINON))
    其中EPSINON 是允许的误差(即精度)假设指针变量的名字为p,它与零值比较的标准if 语句如下:
    if (p == NULL) // p 与NULL 显式比较,强调p 是指针变量
    if (p != NULL)这样可以保证你的语句在任何编译器下都不会存在隐患