c++ - 具有继承的C++中的段错误

标签 c++ pointers vector gdb segmentation-fault

#include<iostream>
#include<vector>  
#include "hashtable.h"

using namespace std;

class dummy
{
public:
int a;
vector<int> k;
dummy()
{
    a= 10;
    k.push_back(10);
    k.push_back(20);
}

~dummy()
{
    cout<<"clearing dummy"<<endl;
    k.clear();
    cout<<"comple"<<endl;
}
 };


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

void create_nodes_hashtable();

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

 };


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

//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);
}
} 


class child: public parent
{
public:
  child()
  {
 }
 ~child()
 {
     cout<<"clearing child"<<endl;
}
};

int main()
{
 child c;
 cout<<"gng of scope"<<endl;
return(0);
}

Hashtable.h代码如下:

 #ifndef _HASHTABLE_H_
 #define _HASHTABLE_H_

 #include<vector>
 #include<iostream>
 #include<string> 
 #include<algorithm>
 //returns this value if the object corresponding to the key is not found in the hashtable
 #define NOT_FOUND_IN_HASHTABLE -1

 //default hashtable size
 //wen the size is not passed in the arguments
  #define DEFAULT_HASHTABLE_SIZE 200//WINDOW_LENGTH

  //defines the data structure hashtable
  //the hashtable assumes that each object_identifier refers to an unique object

  class Hashtable
  {

private :

//counts the number of elements added into the hashtable
unsigned int count_elements_added;

//counts the number of elements removed from the hashtable
unsigned int count_elements_removed;

//counts the number of elements present in the hashtable
unsigned int count_elements_present;

//sets the size of the hashtable
unsigned int hashtable_size;

//the data structure (vector) that contains the objects
 //the position on the hastable is defined by 2 keys
 //one the position in the array of the hashtable : the start of the node is used
//the second is the first element in the pair present in the hash table //end of the node is used
std :: vector< std :: vector<std :: pair<int,int> > > hashtable;

//intialize the hashtable
 void intialize_hashtable();

//checks whether the hashtable is corrupted or not
 //returns true,if the hashtable is corrupted
//else returns false
 bool is_corrupt();

 public :

Hashtable()
{
    hashtable_size = DEFAULT_HASHTABLE_SIZE;
    hashtable.clear();
    intialize_hashtable();

    //counts the number of elements added into the hashtable
    count_elements_added = 0;

    //counts the number of elements removed from the hashtable
    count_elements_removed = 0;

    //counts the number of elements present in the hashtable
    count_elements_present = 0;
};

Hashtable(int hash_table_size)
{
    hashtable.clear();
    hashtable_size = hash_table_size;
    intialize_hashtable();

    //counts the number of elements added into the hashtable
    count_elements_added = 0;

    //counts the number of elements removed from the hashtable
    count_elements_removed = 0;

    //counts the number of elements present in the hashtable
    count_elements_present = 0;
};

//add elemnet to the hashtable
void add_element(int key,int object_identifier,int object_info);

//given the key and the object identifier
//returns the object info
int get_element(int key,int object_identifier);

//delete the element from the hashtable
void remove_element(int key,int object_identifier);

//prints the contents of the hashtable
void print();

void clear_memory()
{
    std::cout<<"clearing hashtable"<<std::endl;
    hashtable.clear();
    hashtable_size = 0;
}

~Hashtable()
{
    hashtable_size = 0;
    hashtable.clear();

    //counts the number of elements added into the hashtable
    count_elements_added = 0;

    //counts the number of elements removed from the hashtable
    count_elements_removed = 0;

    //counts the number of elements present in the hashtable
    count_elements_present = 0;

};
 };

 //initialize the hashtable
 inline void Hashtable :: intialize_hashtable() 
 {
for(unsigned int i = 0;i < hashtable_size;i++)
{
    std :: vector<std :: pair<int,int> > temp;
    temp.clear();
    hashtable.push_back(temp);
}
}

//add elemnet to the hashtable
inline void Hashtable :: add_element(int key,int object_identifier,int object_info)
{   
hashtable[key].push_back(std :: pair<int,int> (object_identifier,object_info));

count_elements_added++;
count_elements_present++;

}

//given the key and the object identifier
//returns the object info
//if the object has not been found then returns the macro : value
inline int Hashtable :: get_element(int key,int object_identifier)
{
//get elements from the hastable that have the same key 
std :: vector<std :: pair<int,int> > same_key_elements = hashtable[key];

//if the hastable array is empty then return not found
if(same_key_elements.empty())
{
    return(NOT_FOUND_IN_HASHTABLE);
}

//scan thru all the elemenets in the hashtable to find if the element is present 
for(std :: vector<std :: pair<int,int> > :: iterator same_key_elements_iter = same_key_elements.begin();same_key_elements_iter != same_key_elements.end();same_key_elements_iter++) 
{
    //check if the object identifier is present in the array
    if((*same_key_elements_iter).first == object_identifier)
    {
        //returns the object info corresponding to the object identifier    
        return((*same_key_elements_iter).second);
    }
}

same_key_elements.clear();


//if the element has not been found then return the default value
return(NOT_FOUND_IN_HASHTABLE);
 }

 //given an key and the element object_identifier,removes the element from the hashtable
 inline void Hashtable :: remove_element(int key,int object_identifier)
 {  
//get elements from the hastable that have the same key 
std :: vector<std :: pair<int,int> > same_key_elements = hashtable[key];

//saves the remanining elements in the array after the element woth the same key has been found
std :: vector<std :: pair<int,int> > remaining_key_elements;
remaining_key_elements.clear();

//if the hastable array is empty then no deletion can take place
if(same_key_elements.empty())
{
    return;
}

//scan thru all the elemenets in the hashtable to find if the element is present 
for(std :: vector<std :: pair<int,int> > :: iterator same_key_elements_iter = same_key_elements.begin();same_key_elements_iter != same_key_elements.end();same_key_elements_iter++) 
{
    //check if the object identifier is not present in the array
    if(!((*same_key_elements_iter).first == object_identifier))
    {
        remaining_key_elements.push_back(std :: pair<int,int> ((*same_key_elements_iter).first ,(*same_key_elements_iter).second));
    }
}

hashtable[key] = remaining_key_elements;
same_key_elements.clear();
remaining_key_elements.clear();

//update the hashtable counts
count_elements_removed++;
count_elements_present--;

}

//checks whether the hashtable is corrupted or not
//returns true,if the hashtable is corrupted
//else returns false
inline bool Hashtable :: is_corrupt()
{
 //the size of the hashtable should be equal to the size it was initialised
if(hashtable.size() != hashtable_size)
 {
    return(true);
 }
//the number of elements added - no. of elements removed should be equal to the no. of elements present in the hashtable
 else if((count_elements_added -count_elements_removed) != count_elements_present) 
 {
     return(true);
 }
 //default case :
 else
{
    return(false);
 }
 }

//prints the contents of the hashtable
inline void Hashtable :: print()
{
 //checks the case where the hashtable may be empty
 if(hashtable.empty())
 {
    std :: cout<<"the hash table is empty"<<std :: endl;
 }

  //printing the contents of the hashtable
  unsigned int pos = 0;
for(std :: vector< std :: vector<std :: pair<int,int> > > :: iterator  hashtable_iter = hashtable.begin();hashtable_iter != hashtable.end();hashtable_iter++,pos++)
 {
     std :: cout<<"key = "<<pos;
    std :: vector<std ::pair<int,int> > tmp =  *hashtable_iter;
     //Tools :: print(tmp);  
     std :: cout<<std :: endl;
}
 }

 #endif

分段故障来了。代码的输出是:

 gng of scope
 clearing child
 clearing parent
 clearing dummy
 comple
 Segmentation fault (core dumped)

请指导如何调试这个问题。我尝试了 gdb,但我不明白段错误的原因。

最佳答案

delete(nodes_hashtable);Parent 中,我认为你没有为 nodes_hashtable 分配内存。

请使用

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

如果没有为其分配内存,此代码将不会为 nodes_hashtable 调用 delete。

关于c++ - 具有继承的C++中的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22112137/

相关文章:

C++:出现奇怪的 header 错误

c++ - 为什么我的程序最多只能编译 16 条记录?

c - 使用qsort在c中指向char *的指针数组

c++ - 如何在不更改原始数组的情况下对指向结构的指针数组进行排序?

arrays - swift 中的矢量对象

c++ - 在 C++ 中发生堆栈溢出时获取 SIGSEGV

c++ 和 mongodb - 无法编译 - 对`boost::system::generic_category() 的 undefined reference

c - 在C中传递非全局数组变量

c++ - 为什么程序在 C++ 中迭代空 vector 时会抛出运行时错误

c++ - 为什么我不能将项目添加到我的 vector 中?