这个析构函数发生了什么? (段错误)

时间:2017-02-16 17:19:07

标签: c++

我正在编写一个旨在模拟珊瑚生长的程序,我希望珊瑚成为某种二叉树。分支相当于节点。当我运行程序时,我遇到了“分段错误”错误,经过测试和做一些研究后我认为这是因为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;
}

1 个答案:

答案 0 :(得分:1)

您要删除leftright分支两次。

考虑CoralBranch的工作原理:拥有 left分支和right分支。由于它拥有分支,因此它可以并且应该在析构函数delete~CoralBranch。它确实如此!好。

您的班级Coral 仅拥有一个对象,trunk。但看看Coral::destroy_coral做了什么!允许删除类Coral 不拥有的分支对象!它应该销毁的唯一CoralBranch就是它所拥有的trunk destroy_coral。那个人将通过析构函数删除自己的子分支,并递归地删除它们自己的子分支。因此,您所要做的就是从destroy_coral中删除递归{{1}}调用。