1

I can't dereference a char**. Specifically in this context: *(array[ currentPos ].element)

array is a vector<HashEntry> type. My hash table holds entries of this type. The HashEntry type contains the following members.

char** element This is actually a template type, but the template I chose was char** so I can store pointers to strings passed in my initTable function rather than making copies and wasting space in my Hash Table.

EntryType info This is just an enum type.

In my class, I am setting the addresses of the pointer to string to element. So element contains the address of the pointer that I inserted. I need to dereference element to get my char* string, but this causes a segfault.

The weird thing is that when printing element in gdb it gives me the exact address of the pointer that points to the target string, but I cannot dereference it.

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

    }
5
  • your elements are pointing to the words array that you pass in to initTable. is it being destroyed afterwards?
    – kmdreko
    Feb 21, 2017 at 0:26
  • No they're not. words are dynamically allocated and initialized inside the main function. Feb 21, 2017 at 0:34
  • Can you include that with your post to make complete and verifiable? you also have table as a local in initTable where findInTable can't access it. (and what is currentPos? its in insert and not findPos) Please post your real code
    – kmdreko
    Feb 21, 2017 at 0:39
  • Sorry, that should have been visible to all functions. I fixed it. Feb 21, 2017 at 0:58
  • The weird thing is that when printing element in gdb it gives me the exact address of the pointer that points to the target string, but I cannot dereference it. Feb 21, 2017 at 1:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.