C++ 取消引用(模板类型)双指针字符会导致segfault
我无法取消对C++ 取消引用(模板类型)双指针字符会导致segfault,c++,segmentation-fault,C++,Segmentation Fault,我无法取消对字符**的引用。特别是在这个上下文中:*(数组[currentPos].element) 数组是一种向量类型。我的哈希表包含这种类型的条目。HashEntry类型包含以下成员 char**element这实际上是一种模板类型,但我选择的模板是char**,因此我可以存储指向在initTable函数中传递的字符串的指针,而不是在哈希表中复制和浪费空间 EntryType info这只是一个枚举类型 在我的类中,我将字符串指针的地址设置为元素。因此元素包含我插入的指针的地址。我需要取消对
字符**
的引用。特别是在这个上下文中:*(数组[currentPos].element)
数组
是一种向量
类型。我的哈希表包含这种类型的条目。HashEntry
类型包含以下成员
char**element
这实际上是一种模板类型,但我选择的模板是char**
,因此我可以存储指向在initTable
函数中传递的字符串的指针,而不是在哈希表中复制和浪费空间
EntryType info
这只是一个枚举类型
在我的类中,我将字符串指针的地址设置为元素
。因此元素
包含我插入的指针的地址。我需要取消对元素的引用以获取我的char*字符串,但这会导致segfault
奇怪的是,当在gdb中打印元素时,它会给我指向目标字符串的指针的确切地址,但我无法取消引用它
QuadraticHashTable<char**> table(NULL, 101); //Global Hash Table
//Main
int main()
{
char ** docWords;
char *s; //ifstream string buffer
//read from ifstream and store everything that is read inside @s as a buffer
//code that does the reading.
//code that does the reading.
//code that does the reading.
docWords[0] = strtok(s, "\n");
//Read document
for (int i = 1; i < docSize; i++)
docWords[i] = strtok(NULL, "\n\r");
initTable(docWords, docSize);
char ** wordsToFind;
//Initialize wordsToFind to look up in hash table
wordsToFind[0] = new char*[100];
wordsToFind[0] = "some word";
//repeat with the rest of the words that needs to be found
findInTable(wordsToFind, size_of_wordsToFind);
}
//Function that init table
void initTable(char * words[], int size)
{
//Insert words
for (int i = 0; i < size; i++)
{
table.insert(&words[i]);
}
}
void findInTable(char * words[], int size)
{
if (table.find(&word[0]) == NULL)
{
//the word has not been found
}
}
//QuadraticHashTable.h
//Class Definition
template <class HashedObj>
class QuadraticHashTable
{
public:
explicit QuadraticHashTable( const HashedObj & notFound, int size = 101 );
QuadraticHashTable( const QuadraticHashTable & rhs )
: array( rhs.array), ITEM_NOT_FOUND( rhs.ITEM_NOT_FOUND ),
currentSize( rhs.currentSize ) { }
//stuff
void insert( const HashedObj & x );
enum EntryType { ACTIVE, EMPTY, DELETED };
private:
struct HashEntry
{
HashedObj element; //created with char** type
EntryType info;
};
int findPos( const HashedObj & x ) const;
vector<HashEntry> array;
};
//QuadraticHashTable.cpp
template <class HashedObj>
int QuadraticHashTable<HashedObj>::findPos( const HashedObj & x ) const
{
//Stuff
cout << "array[ currentPos ].element: " << *(array[ currentPos ].element) << "\n"; //SEGFAULT OCCURS HERE
//Stuff
}
template <class HashedObj>
void QuadraticHashTable<HashedObj>::insert( const HashedObj & x )
{
// Insert x as active
int currentPos = findPos( x );
array[ currentPos ] = HashEntry( x, ACTIVE );
//Stuff
}
quadrachichashtable(NULL,101)//全局哈希表
//主要
int main()
{
文字;
char*s;//ifstream字符串缓冲区
//从ifstream读取并将@s中读取的所有内容存储为缓冲区
//进行读取的代码。
//进行读取的代码。
//进行读取的代码。
docWords[0]=strtok(s,“\n”);
//阅读文件
对于(int i=1;i 您的元素是否指向传递给initTable
的单词数组?它是否随后被销毁?不,它们不是。单词在主函数中被动态分配和初始化。您是否可以将其包含在帖子中以使其完整且可验证?您还可以将表格
作为本地ininitTable
,其中findInTable
无法访问它。(什么是currentPos
?它在插入而不是findPos
)请发布您的真实代码抱歉,这应该对所有函数都可见。我修复了它。奇怪的是,当在gdb中打印元素时,它会给我指向目标字符串的指针的确切地址,但我无法取消对它的引用。您的元素指向传递给的单词
数组e> initTable
。之后它会被销毁吗?不,不会。单词在主函数中是动态分配和初始化的。你能在帖子中包含它以使其完整和可验证吗?你也可以在initTable
中将table
作为本地文件,其中findInTable
无法访问它。(什么是currentPos
?它在insert
中,而不是findPos
)请发布您的真实代码抱歉,所有函数都应该可以看到。我已经修复了它。奇怪的是,在gdb中打印元素时,它会给出指向目标字符串的指针的确切地址,但我无法取消引用它。