在 C++ 中使用虚方法导致分段错误

发布于 2024-09-27 19:18:34 字数 2737 浏览 1 评论 0原文

我得到了一个运行正常的 C++ 面向对象程序。我决定通过添加一些 polimorphysm 来修改它,用虚拟方法定义类层次结构。当我调用虚拟方法时,它会产生错误分段错误,可能是因为对象中有垃圾。

这是调用和热身

  GPUAntColony *colony; // Base class
  GPUAntColonyConfiguration config;
  set_config(config);
  set_initial_pheromone(problem, config);
  colony = (GPUAntColony *)new GPUSimpleAntColony(problem, config);//inhereted class
  colony->run(); //Virtual method

现在让我向您展示基类

 class GPUAntColony {

 private:

    void reset_ants() {
    for(unsigned int i=0; i<configuration_.number_of_ants;i++) {
         ants_[i]= Util::random_number(problem_->number_of_vertices());
    }
  }

  void initialize_Pheromone(){
   for(unsigned int i=0; i<problem_->number_of_vertices()*problem_->number_of_vertices();i++) {
       pheromones_[i]=(float)configuration_.initial_pheromone;
   }
  }


   protected:
  float * pheromones_;
    float alpha_;
    float beta_;
  unsigned int iterations;
    GPUAntColonyConfiguration::LocalSearchType local_search_type_;
  GPUAntColonyConfiguration configuration_;
  unsigned int * ants_;
    GPUOptimizationProblem *problem_;

  public:  


 ///Class Constructor for the Class GPU Ant Colony        
 GPUAntColony(GPUOptimizationProblem *problem, const GPUAntColonyConfiguration &config){

   iterations=4096; 
      problem_ = problem; // Including distance array
   configuration_ = config;
   ants_= (unsigned int*) malloc(config.number_of_ants*sizeof(unsigned int));   
      pheromones_ = (float *) malloc(problem->number_of_vertices()*problem->number_of_vertices()*sizeof(float)); 
   alpha_ = config.alpha;
   std::cout << "alpha_ " << alpha_ << std::endl;
      beta_ = config.beta;
      local_search_type_ = config.local_search;
 }

  virtual void run();  

  virtual ~GPUAntColony() {
      delete problem_;
      free(ants_);
      free (pheromones_);
    };


};

子类的定义

class GPUSimpleAntColony : public GPUAntColony{
public:
    GPUSimpleAntColony(GPUOptimizationProblem *problem, const GPUAntColonyConfiguration &config);
    void run();
};

最后是此类方法的实现

void GPUAntColony::run(){ 
  reset_ants();
  initialize_Pheromone();  
}


GPUSimpleAntColony::GPUSimpleAntColony(GPUOptimizationProblem *problem, const GPUAntColonyConfiguration &config):GPUAntColony(problem, config) {
}



void GPUSimpleAntColony::run() {
 GPUAntColony::run();
  antColonyGPULauncher(configuration_.number_of_ants, problem_->number_of_vertices(), problem_->get_distances(), pheromones_,ants_,alpha_, beta_,
             configuration_.evaporation_rate, iterations, 0, 0, 0, 0, ACO_SIMPLE);
}

希望您能帮助我。

非常感谢。

I got a C++ object oriented program that was working right. I have decided to modify it by adding some polimorpysm definining a class hierarchy with virtual methods. When I call the virtual method it produces an fault segmentation error, likely because I have trash in the object.

This is the call and the warming up

  GPUAntColony *colony; // Base class
  GPUAntColonyConfiguration config;
  set_config(config);
  set_initial_pheromone(problem, config);
  colony = (GPUAntColony *)new GPUSimpleAntColony(problem, config);//inhereted class
  colony->run(); //Virtual method

Now let me show you the base class

 class GPUAntColony {

 private:

    void reset_ants() {
    for(unsigned int i=0; i<configuration_.number_of_ants;i++) {
         ants_[i]= Util::random_number(problem_->number_of_vertices());
    }
  }

  void initialize_Pheromone(){
   for(unsigned int i=0; i<problem_->number_of_vertices()*problem_->number_of_vertices();i++) {
       pheromones_[i]=(float)configuration_.initial_pheromone;
   }
  }


   protected:
  float * pheromones_;
    float alpha_;
    float beta_;
  unsigned int iterations;
    GPUAntColonyConfiguration::LocalSearchType local_search_type_;
  GPUAntColonyConfiguration configuration_;
  unsigned int * ants_;
    GPUOptimizationProblem *problem_;

  public:  


 ///Class Constructor for the Class GPU Ant Colony        
 GPUAntColony(GPUOptimizationProblem *problem, const GPUAntColonyConfiguration &config){

   iterations=4096; 
      problem_ = problem; // Including distance array
   configuration_ = config;
   ants_= (unsigned int*) malloc(config.number_of_ants*sizeof(unsigned int));   
      pheromones_ = (float *) malloc(problem->number_of_vertices()*problem->number_of_vertices()*sizeof(float)); 
   alpha_ = config.alpha;
   std::cout << "alpha_ " << alpha_ << std::endl;
      beta_ = config.beta;
      local_search_type_ = config.local_search;
 }

  virtual void run();  

  virtual ~GPUAntColony() {
      delete problem_;
      free(ants_);
      free (pheromones_);
    };


};

The definition of the child class

class GPUSimpleAntColony : public GPUAntColony{
public:
    GPUSimpleAntColony(GPUOptimizationProblem *problem, const GPUAntColonyConfiguration &config);
    void run();
};

And finally the implementation of such method

void GPUAntColony::run(){ 
  reset_ants();
  initialize_Pheromone();  
}


GPUSimpleAntColony::GPUSimpleAntColony(GPUOptimizationProblem *problem, const GPUAntColonyConfiguration &config):GPUAntColony(problem, config) {
}



void GPUSimpleAntColony::run() {
 GPUAntColony::run();
  antColonyGPULauncher(configuration_.number_of_ants, problem_->number_of_vertices(), problem_->get_distances(), pheromones_,ants_,alpha_, beta_,
             configuration_.evaporation_rate, iterations, 0, 0, 0, 0, ACO_SIMPLE);
}

Hopefully you can help me.

Many thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

瘫痪情歌 2024-10-04 19:18:37

谢谢大家,但这绝对是一件愚蠢的事情。我有一个影响执行的静态变量。现在工作正常,

非常感谢您的

欢呼

Thank you every one mates, but it was absolutely a silly thing as always. I had a statical variable that was affecting the execution. Now is working perfectly,

Thank you a lot

cheers

再可℃爱ぅ一点好了 2024-10-04 19:18:37

在基类中 make run 纯虚拟或为其提供默认实现。

virtual void run() = 0;

在子类中,声明 run virtual。

virtual void run();

编辑:真正的问题是基类 run() 不是纯虚拟的。正如下面的评论中提到的,子类 run() 将自动变为虚拟,但我认为显式声明它仍然更清楚。

http://codepad.org/6MlrH5Q4

In the base class make run pure virtual or provide a default implementation for it.

virtual void run() = 0;

In the child class, declare run virtual.

virtual void run();

EDIT: The real problem is that the base class run() isn't pure virtual. As mentioned in comments below the child class run() will automatically become virtual, but I think it is still clearer to explicitly declare it as such.

Compare http://codepad.org/6MlrH5Q4 to http://codepad.org/lBWaEefT

悲念泪 2024-10-04 19:18:36

我注意到您使用 malloc 分配内存而不检查返回值。你有可能返回 NULL 吗?特别是对于 pheromones_ ,它似乎需要 n*n 空间,并且您的示例代码没有给我们值 n。

I notice you're allocating memory with malloc without checking the return value. Any chance you're getting back a NULL? Especially with pheromones_ which appears to require n*n space, and your sample code does not give us the value n.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文