Patrick Emery
Patrick Emery

Reputation: 13

Segfault Error in Custom Dictionary Class C++

So, as part of my assignment in Computer Science, which was to read tweets and put them into a custom Dictionary, I had to, you guessed it, create a dictionary. However, during testing with the dictionary, I encountered an error which I have been unable to fix, despite hours of attempted troubleshooting. I have narrowed it down, and determined that the error lies on line 144, somewhere in the statement cout<<j.get("name").getFront()->getText();, but I have been unable to determine which part of this causes issues, even when breaking it down by parts, except that it begins when I add in the ->getText(), however I heavily suspect that the problem starts earlier on.

I am sorry if I am not too specific, or if I ramble too much, I have just been having trouble with this for a while, and am beginning to get frustrated.

I understand not all the execution or style is the best, so I may ask you to refrain from leaving comments on the way things are done, unless it may directly relate to the problem at hand.

Thank you for any and all help.

/*********************************************************************************************************************
 * [REDACTED]                                                                                                        *
 * CS 101-- Project 4 (Hashing Twitter)                                                                              *
 * This program stores Twitter posts in a hash table                                                                 *                                                                                                        *
 *********************************************************************************************************************/

#include <iostream>
#include <stdlib.h>
#include <vector>

using namespace std;
class tweet {
    private:
        string create_at;
        string text;
        string screen_name;
    public:
        string getCreate_at() {
            return create_at;
        };
        string getText() {
            return text;
        };
        string getScreen_name() {
            return screen_name;
        };
        void setCreate_at(string c) {
            create_at=c;
        };
        void setText(string c) {
            text=c;
        };
        void setScreen_name(string c) {
            screen_name=c;
        };
};
class LinkedList {
    public:
        tweet* getFront() {
            return top;
        };
        LinkedList* getNext() {
            return next;
        };
        void setNext(LinkedList* c) {
            next = c;
        };
        void setTweet(tweet c) {
            top = &c;
        };
        void setTweet(tweet* c) {
            top = c;
        };
        void insertFront(tweet c) {
            LinkedList temp;
            temp.setTweet(top);
            temp.setNext(next);
            this->setTweet(c);
            this->setNext(&temp);
        };
        tweet* removeFront() {
            tweet* temp;
            temp = top;
            if(next != NULL){
                top = next->getFront();
                if(next->getNext() != NULL)
                    next = next->getNext();
            }
            return temp;
        };
    private:
        tweet* top;
        LinkedList* next;
};
class HashTable {
    private:
        vector<LinkedList> store [256];//access by firstcharacter of name as index of array then search through vector linearly until find key 
        LinkedList getLinkedList(string c) {
            vector<LinkedList> temp=store[(int)c.c_str()[0]];
            for(int i =0;i<temp.size();i++) {
                if(temp.at(i).getFront()->getScreen_name()==c) {
                    return temp.at(i); //gets list of tweets
                }
            };
        };
        bool keyExists(string c) {
            vector<LinkedList> temp = store[(int)c.c_str()[0]];
            for(int i =0;i<temp.size();i++) {
                if(temp.at(i).getFront()->getScreen_name()==c) {
                    return true; //gets list of tweets
                }
            };
            return false;
        };
        void insertTweet(tweet c){
            if(keyExists(c.getScreen_name())){
                getLinkedList(c.getScreen_name()).insertFront(c);
            } else {
                LinkedList temp;
                temp.setTweet(c);
                store[c.getScreen_name().c_str()[0]].push_back(temp);
            }
        };
    public:
        void put(tweet c) {
            insertTweet(c);
        };
        LinkedList get(string key) {
            return getLinkedList(key);
        };
        bool contains(string key) {
            return keyExists(key);
        };
        void remove(string key) {
            vector<LinkedList> temp=store[key.c_str()[0]];
            for(int i =0;i<temp.size();i++) {
                if(temp.at(i).getFront()->getScreen_name()==key) {
                    temp.erase(temp.begin()+i); //gets list of tweets
                }
            };
        };
};
HashTable parser(string filename) {
    //backslashes
};
int main(int argc, char *argv[])
{
    tweet hello;
    hello.setText("hello");
    hello.setScreen_name("user");
    hello.setCreate_at("10211997");
    tweet heyo;
    heyo.setText("heyo");
    heyo.setScreen_name("name");
    heyo.setCreate_at("79912101");
    LinkedList jerome;
    jerome.insertFront(hello);
    cout<<jerome.getFront()->getText()<<endl;
    jerome.insertFront(heyo);
    cout<<jerome.removeFront()->getText()<<endl;
    HashTable j;
    j.put(heyo);
    cout<<j.get("name").getFront()->getText();
}

Upvotes: 1

Views: 52

Answers (1)

bmanga
bmanga

Reputation: 140

You are getting the addresses of temporaries:

    void insertFront(tweet c) {
        LinkedList temp;
        temp.setTweet(top);
        temp.setNext(next);
        this->setTweet(c); //should be &c, but c is a temporary!
        this->setNext(&temp); //temp is a temporary!
    };

Also, in HashTable, you need put and insertTweet to have a tweet& parameter.

Finally, still in insertTweet, you should pass the address of c to setTweet.

Note that this code is very fragile, as you will have dangling pointers as soon as the tweet objects go out of scope.

Upvotes: 3

Related Questions