C++ c+中的分段错误+;继承

C++ c+中的分段错误+;继承,c++,pointers,vector,gdb,segmentation-fault,C++,Pointers,Vector,Gdb,Segmentation Fault,请指导如何调试此问题。我尝试了gdb,但是我不理解分段错误的原因。delete(nodes\u hashtable)在Parent中,我认为您没有为节点分配内存\u hashtable 请使用 gng of scope clearing child clearing parent clearing dummy comple Segmentation fault (core dumped) parent() { nodes_hashtable=NULL; p=新假人(); } 虚拟父

请指导如何调试此问题。我尝试了gdb,但是我不理解分段错误的原因。

delete(nodes\u hashtable)
Parent
中,我认为您没有为
节点分配内存\u hashtable

请使用

 gng of scope
 clearing child
 clearing parent
 clearing dummy
 comple
 Segmentation fault (core dumped)
parent()
{
nodes_hashtable=NULL;
p=新假人();
}
虚拟父对象()
{
无法
删除(nodes_hashtable);
Parent
中,我认为您没有为
节点_hashtable
分配内存

请使用

 gng of scope
 clearing child
 clearing parent
 clearing dummy
 comple
 Segmentation fault (core dumped)
parent()
{
nodes_hashtable=NULL;
p=新假人();
}
虚拟父对象()
{
cout
nodes\u哈希表指向null,这将导致内核转储释放null内存
班级家长
{
公众:
虚拟*p;
哈希表*节点\u哈希表;
父项()
{
p=新假人();
nodes_hashtable=NULL;
}
void创建_节点_哈希表();
虚拟父对象()
{
cout
nodes\u哈希表指向null,这将导致内核转储释放null内存
班级家长
{
公众:
虚拟*p;
哈希表*节点\u哈希表;
父项()
{
p=新假人();
nodes_hashtable=NULL;
}
void创建_节点_哈希表();
虚拟父对象()
{

cout
节点\u哈希表
未初始化,因此此指针可以包含任何数据。您必须在
父级
构造函数中初始化它:

nodes_hashtable is point to null which would cause core dump to free null memory。


class parent
{
public:
dummy *p;
Hashtable *nodes_hashtable;
parent()
{
    p = new dummy();
nodes_hashtable=NULL;
}

void create_nodes_hashtable();

virtual ~parent()
{
    cout<<"clearing parent"<<endl;
    delete(p);
    if(nodes_hashtable != NULL)
        delete(nodes_hashtable);
}

 };

 //given the hashtable,updates the values in the hashtable
 void parent ::  create_nodes_hashtable()
{
if(nodes_hashtable  == NULL)
    nodes_hashtable = new(std::nothrow) Hashtable(500);
else 
    return;

//check :if the hashtable has been created
if(NULL == nodes_hashtable)
{
    cout<<"out of memory"<<endl;
}

for(unsigned int i = 0;i<20 ;i++)
{       
    //normalising the key ,so that the key lies within the range of the hashtable
    int key = i;//get_nodes_hashtablekey(i,i);
    (*nodes_hashtable).add_element(key,i,i);
}
} 
在进一步的代码中具有可预测的条件。
在大多数编译器不检查空指针的情况下,您也可以删除空指针,因此没有明显的理由更改
父级
析构函数

节点\u哈希表
未初始化,因此此指针可以包含任何数据。您必须在
父级
构造函数中初始化它:

nodes_hashtable is point to null which would cause core dump to free null memory。


class parent
{
public:
dummy *p;
Hashtable *nodes_hashtable;
parent()
{
    p = new dummy();
nodes_hashtable=NULL;
}

void create_nodes_hashtable();

virtual ~parent()
{
    cout<<"clearing parent"<<endl;
    delete(p);
    if(nodes_hashtable != NULL)
        delete(nodes_hashtable);
}

 };

 //given the hashtable,updates the values in the hashtable
 void parent ::  create_nodes_hashtable()
{
if(nodes_hashtable  == NULL)
    nodes_hashtable = new(std::nothrow) Hashtable(500);
else 
    return;

//check :if the hashtable has been created
if(NULL == nodes_hashtable)
{
    cout<<"out of memory"<<endl;
}

for(unsigned int i = 0;i<20 ;i++)
{       
    //normalising the key ,so that the key lies within the range of the hashtable
    int key = i;//get_nodes_hashtablekey(i,i);
    (*nodes_hashtable).add_element(key,i,i);
}
} 
在进一步的代码中具有可预测的条件。
您也可以在大多数编译器不检查空指针的情况下删除空指针,因此没有重要的理由更改
父对象
析构函数

,您需要遵循。您应该初始化所有指针,以便可以对其调用
delete
。使它们指向动态分配的对象,或者将它们设置为
nullptr
,(或者
0
或者
NULL
如果是C++11之前的版本)您需要遵循。并且您应该初始化所有指针,这样您就可以对它们调用
delete
。或者让它们指向动态分配的对象,或者将它们设置为
NULL
,(或者
0
或者
NULL
如果是C++11之前的版本。)我认为
create_nodes_hashtable
没有被调用,在代码块中只创建子对象,没有其他操作。@JKB仍然是一个不完整的答案,和其他答案一样。我认为
create_nodes_hashtable
没有被调用,在代码块中只创建子对象,没有o操作就这样完成了。@JKB仍然是一个不完整的答案,就像其他答案一样。尽管代码仍然严重损坏。这只是解决了一个特定的问题。如果父级
构建或分配了副本,它就会爆炸。如果代码工作正常,则代码不会被破坏。也就是说,三个规则是一个很好的预防方法你正在自食其果。浅层复制问题可以通过重写复制构造的函数和运算符=,或智能指针来解决。或者将类定义为Singleton虽然代码仍然严重损坏。这只是修复了一个特定的问题。如果父级
构建或分配了副本,它将爆炸。代码不是br好的,如果它工作正常的话。也就是说,三的规则是一个很好的主意,可以防止你自食其果。浅层复制问题可以通过重写复制构造的函数和运算符=,或智能指针来解决。或者将类定义为singleton
nodes_hashtable is point to null which would cause core dump to free null memory。


class parent
{
public:
dummy *p;
Hashtable *nodes_hashtable;
parent()
{
    p = new dummy();
nodes_hashtable=NULL;
}

void create_nodes_hashtable();

virtual ~parent()
{
    cout<<"clearing parent"<<endl;
    delete(p);
    if(nodes_hashtable != NULL)
        delete(nodes_hashtable);
}

 };

 //given the hashtable,updates the values in the hashtable
 void parent ::  create_nodes_hashtable()
{
if(nodes_hashtable  == NULL)
    nodes_hashtable = new(std::nothrow) Hashtable(500);
else 
    return;

//check :if the hashtable has been created
if(NULL == nodes_hashtable)
{
    cout<<"out of memory"<<endl;
}

for(unsigned int i = 0;i<20 ;i++)
{       
    //normalising the key ,so that the key lies within the range of the hashtable
    int key = i;//get_nodes_hashtablekey(i,i);
    (*nodes_hashtable).add_element(key,i,i);
}
} 
parent() :
    p(new dummy()),
    nodes_hashtable(NULL)
{}