2017-02-16 195 views
0

我正在編寫一個旨在模擬珊瑚生長的程序,我希望珊瑚是某種二叉樹。分支是節點的等價物。當我運行程序時,我得到了「分段錯誤」錯誤,經過測試並做了一些研究後,我在想這是因爲Coral類的析構函數試圖刪除一個NULL對象。但是我不知道如何解決它,因爲我不完全理解析構函數是如何工作的,而且我對類的概念並不熟悉。這個析構函數發生了什麼? (段錯誤)

我給你頭文件,其中聲明瞭CoralBranch和Coral類,實現類的.cpp和main.cpp。

謝謝大家提前。

工作在一個最小,完整和可驗證的例子,再現我的問題,我遇到了沒有問題。我認爲我的程序中存在其他錯誤,這些錯誤在應該沒有問題的方法上挑起了分段錯誤。

coral2.h

#ifndef __CORAL2_H_INCLUDED__ 
#define __CORAL2_H_INCLUDED__ 

class CoralBranch{ 

    public: 

CoralBranch(); // class constructor 1 
CoralBranch(double xpos, double zpos, double th); // class constructor 2 
~CoralBranch(); 

CoralBranch *left; // pointer to left child 
CoralBranch *right; // pointer to right child 
double x; // x-position of the beggining of the branch 
double z; // z-position of the beggining of the branch 
double theta; // branch's direction: angle between the branch and z-axis 
double length; // branch's length 
int state; // state==1: branch is able to grow, state==0: branch can't grow 

}; 


class Coral{ 

    public: 

Coral(); // class constructor 1 
Coral(double bl, double gr, double ir, double dt); // class constructor 2 
~Coral(); // class destructor 

// adds theta-oriented left son to coral-branch *leaf 
void add_left(CoralBranch *leaf, double theta); 
// adds theta-oriented right son to coral-branch *leaf 
void add_right(CoralBranch *leaf, double theta); 
// destroys coral-branch *branch and its decendents 
void destroy_coral(CoralBranch *branch); 
// tests if the growing branch influence zone penetrates testbranch influence zone 
void distance_test(Coral crl, CoralBranch *growingbranch, CoralBranch *testbranch); 
// tests if the growing branch is big enough to have children and adds them 
void branching_test(Coral crl, CoralBranch *growingbranch, double thetaleft, double thetaright); 
// applies grow mechanism to growing branch 
void branch_grow(Coral crl, CoralBranch *growingbranch); 
// simulates the growing process for the entire colony after one time step by 
// applying "branch_grow" to the whole colony 
void coral_grow(Coral crl, CoralBranch *growingbranch); 

    //private: 

CoralBranch *trunk; 

double branching_length; 
double growth_rate; 
double influence_radius; 
double Deltat; 


}; 

#endif 

coral2.cpp(僅構造和析構)

CoralBranch::CoralBranch():left(NULL),right(NULL),x(0.0),z(0.0),theta(0.0),length(1.0),state(1) 
{ 
// there isn't any methods in this class 
} 

CoralBranch::CoralBranch(double xpos, double zpos, double th):left(NULL),right(NULL),x(xpos),z(zpos),theta(th),length(1.0),state(1) 
    { 

    } 

CoralBranch::~CoralBranch(){ 

    delete left; 
    delete right; 

} 

Coral::Coral(){ 

    trunk=new CoralBranch(); 

    branching_length=1.0; 
    growth_rate=1.0; 
    influence_radius=1.0; 
    Deltat=1.0; 

} 

Coral::Coral(double bl, double gr, double ir, double dt){ 

trunk=new CoralBranch(); 
    //trunk=NULL; 

    branching_length=bl; 
    growth_rate=gr; 
    influence_radius=ir; 
    Deltat=dt; 

} 

Coral::~Coral(){ // coral destruction 

    destroy_coral(trunk); 

} 

void Coral::destroy_coral(CoralBranch *branch){ // destroys branch *branch and its decendents 

    if(branch == NULL) return; 

    destroy_coral(branch->left); 
    destroy_coral(branch->right); 
    delete branch; 

    /*if(branch!=NULL){ 

    destroy_coral(branch->left); // destruction advance 
    destroy_coral(branch->right); 
    delete branch; // currently considered branch is deleted if it exists 

    }*/ 
} 

的main.cpp

#include <iostream> 
#include <stdio.h> 
#include <stdlib.h> 
#include "coral2.h" 
#include <math.h> 
using namespace std; 

int main(){ 

    /* Variable declaration, these variables need to be initialized in order to 
    construct the coral with the characteristics we desire */ 
    double branchinglength, growthrate, influenceradius, deltat; 
    /* Declaration and initialization of the total simulation time */ 
    double TotalTime=2; 
    /* Declaration and initialization of counters */ 
    int i=0; 
    int j=0; 
    printf("%d\n",j); 

    /* Initialization of Coral class parameters*/ 
    branchinglength=0.2; 
    growthrate=1.0; 
    influenceradius=0.1; 
    deltat=1.0; 

    /* Creation of the coral object*/ 
    static Coral GrowingCoral(branchinglength, growthrate, influenceradius, deltat); 

    /* Starting growth process */ 
    for(i=0; i<TotalTime; i=i+deltat){ 

    j++; 
    printf("%d\n",j); 

    GrowingCoral.coral_grow(GrowingCoral, GrowingCoral.trunk); 

    } 

    printf("%f\n",GrowingCoral.branching_length); 

    return 0; 
} 
+2

歡迎來到Stack Overflow。請花些時間閱讀[The Tour](http://stackoverflow.com/tour),並參閱[幫助中心](http://stackoverflow.com/help/asking)中的資料,瞭解您可以在這裏問。 –

+3

解決此類問題的正確工具是您的調試器。在*堆棧溢出問題之前,您應該逐行執行您的代碼。如需更多幫助,請閱讀[如何調試小程序(由Eric Lippert撰寫)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。至少,您應該\編輯您的問題,以包含一個[最小,完整和可驗證](http://stackoverflow.com/help/mcve)示例,該示例再現了您的問題,以及您在調試器。 –

+4

_「,因爲Coral類的析構函數正試圖刪除一個NULL對象。」使用'NULL'與'NULL'或'nullptr'完全正確。雖然調用'delete'不會將指針值設置爲NULL或nullptr. –

回答

1

您正在刪除leftright分支兩次。

想想看,你的作品CoralBranch:它擁有一個left分支和right分支。既然它擁有的分支機構,它可以並且應該delete他們在破壞者~CoralBranch。它確實!好。

你的類Coral擁有只有一個對象時,trunk。但看看Coral::destroy_coral做什麼!允許刪除Coral不屬於的分支對象!它應該銷燬的唯一CoralBranch是它擁有的trunk。那個會通過析構函數刪除它自己的子分支,以及遞歸地刪除它們自己的子分支。因此,您所要做的就是從destroy_coral中刪除遞歸destroy_coral調用。

+0

感謝您的解釋。我做了你說的話,並且我沒有更多的問題'Coral :: destroy_coral'。然而,我現在在'CoralBranch ::〜CoralBranch()'析構函數中遇到一個錯誤。在一個最小的例子中編輯我的初始文章,我發現運行該程序沒有問題。我認爲我的代碼有其他問題... – bengo