在 C++ 中使用虚方法导致分段错误
我得到了一个运行正常的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
谢谢大家,但这绝对是一件愚蠢的事情。我有一个影响执行的静态变量。现在工作正常,
非常感谢您的
欢呼
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
在基类中 make run 纯虚拟或为其提供默认实现。
在子类中,声明 run virtual。
编辑:真正的问题是基类 run() 不是纯虚拟的。正如下面的评论中提到的,子类 run() 将自动变为虚拟,但我认为显式声明它仍然更清楚。
将 http://codepad.org/6MlrH5Q4 与
In the base class make run pure virtual or provide a default implementation for it.
In the child class, declare run virtual.
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
我注意到您使用
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 withpheromones_
which appears to require n*n space, and your sample code does not give us the value n.