Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Linked list SIGSEGV, Segmentation fault

I was doing a practice problem using linked lists (I wanted to practice them a bit more) and I got the following error

Program received signal SIGSEGV, Segmentation fault.
0x0000555555555888 in LinkedList::getLink (this=0x0) at main.cpp:24 24
return link;

I can’t tell what the problem with this method is,since looking back on the past things I wrote they seemed to be the same as this one.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#include<iostream>
#include<string>

class LinkedList{
    char sign;
    int count;
    LinkedList *link;
public:
    LinkedList(char sign) : sign(sign),count(1),link(nullptr) {}
    inline void Increment()
    {
        count++;
    }
    inline int getCount() const
    {
        return count;
    }
    inline void setLink(LinkedList *whereTo)
    {
        link=whereTo;
    }
    inline LinkedList* getLink() const
    {
        return link;
    }
    inline char getSign() const
    {
        return sign;
    }
};

void stringInput(std::string &var)
{
    std::cout<<"Enter some text:";
    std::getline(std::cin,var);
}

unsigned int factorial(unsigned int n)
{
    return (n!=0)? n*factorial(n-1) : 1;
}

void addList(char sign,LinkedList *&start,LinkedList *&helper,LinkedList *&end)
{
    end=new LinkedList(sign);
    if(start==nullptr)
    {
        start=end;
    }
    else
    {
        helper->setLink(end);
    }
    helper=end;
}

bool isInList(char sign,LinkedList *helper)
{
    while(helper!=nullptr)
    {
        if(sign==helper->getSign())
        {
            helper->Increment();
            return true;
        }
        helper=helper->getLink();
    }
    return false;
}

void addSignsToList(const std::string &var,LinkedList *&start,LinkedList *&helper,LinkedList *&end)
{
    for(int i=0;i<var.size();i++)
    {
        if(!isInList(var[i],start))
        {
            addList(var[i],start,helper,end);
        }
    }
}

void freeLinkedLists(LinkedList *start)
{
    LinkedList *helper=start->getLink();
    while(start!=nullptr)
    {
        delete start;
        start=helper;
        helper=helper->getLink();
    }
}

unsigned int factorialSum(LinkedList *helper)
{
    unsigned int sum=1;
    while(helper!=nullptr)
    {
        sum*=factorial(helper->getCount());
        helper=helper->getLink();
    }
    return sum;
}

unsigned int comb(const std::string &var)
{
    LinkedList *start=nullptr,*helper=nullptr,*end=nullptr;
    addSignsToList(var,start,helper,end);
    unsigned int upperHalf=factorial(var.size());
    double lowerHalf=factorialSum(start);
    freeLinkedLists(start);
    return upperHalf/lowerHalf;
}

int main()
{
    std::string var;
    stringInput(var);
    std::cout<<"The word \""<<var<<"\" has "<<comb(var)<<" possible combinations!\n";
    std::cin.get();
    return 0;
}

>Solution :

In

void freeLinkedLists(LinkedList *start)
{
    LinkedList *helper=start->getLink(); // fails immediately if start is null
    while(start!=nullptr)
    {
        delete start;
        start=helper;
        helper=helper->getLink(); // too late. Helper may already be null. 
                                  // This won't be  spotted until start is 
                                  // tested on the next loop iteration 
    }
}

Instead use

void freeLinkedLists(LinkedList *start)
{
    while(start!=nullptr) // handles empty list case
    {
        LinkedList *helper=start->getLink(); // get next node while we know 
                                             // current node is valid
        delete start;
        start=helper; // may be null and will be caught by the while
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading