Segmentation error using virtual method in C ++

I have an object oriented C ++ program that works correctly. I decided to change it by adding some polymorphism that defines the class hierarchy with virtual methods. When I call the virtual method, it causes an error segmentation error, probably because I have garbage in the object.

This is a challenge and 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 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_);
    };


};

Child class definition

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

And finally, the implementation of such a 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);
}

I hope you can help me.

Thank you very much in advance.

+3
source share
3 answers

, malloc . NULL? pheromones_, , , n * n , n.

+1

, , , . , . ,

+1

make run pure virtual .

virtual void run() = 0;

.

virtual void run();

EDIT: , run() . , run() , , .

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

0
source

Source: https://habr.com/ru/post/1770140/


All Articles