-3

I AM NOT ALLOWED TO USE STL CONTAINERS!

I am getting a Segmentation fault from the SortedListClass(const SortedListClass< T > &rhs), operator=(const SortedListClass< T > &rhs), and clear() functions in the following code.

This is the result from the debugger:

#0  __GI___libc_free (mem=0x4) at malloc.c:2929
#1  0x00000000004016d6 in SortedListClass::clear (this=0x7ffffffde470) at SortedListClass.inl:45
#2  0x00000000004011b5 in SortedListClass::operator= (this=0x7ffffffde470, rhs=...) at SortedListClass.inl:28
#3  0x0000000000400c88 in main () at project5.cpp:33

SortedListClass.inl line 45 is the following: delete tempHead;

SortedListClass.inl line 28 is the following: this->clear();

project5.cpp line 33 is the following: copyTestList = testList;

Here is the code:

SortedListClass.inl

template<class T>
SortedListClass< T >::SortedListClass()
{
     head = NULL;
     tail = NULL;
     cout<<"Empty list intialized...."<<endl; 
}

template<class T>
SortedListClass< T >::SortedListClass(const SortedListClass< T > &rhs)
{
    cout<<"Copied Value(s)"<<endl; 
    LinkedNodeClass< T > *rhsFront;
    LinkedNodeClass< T > *newCopyNode;
    rhsFront = rhs.head;
    while(rhsFront != NULL)
    {
        newCopyNode = new LinkedNodeClass< T >(rhsFront,rhsFront->getValue(),
        rhsFront->getNext());
        rhsFront = newCopyNode;
        rhsFront = rhsFront->getNext();
    } 
}

template<class T> 
void SortedListClass< T >::operator=(const SortedListClass< T > &rhs)
{
    this->clear();
    *this = SortedListClass(rhs);
}

template<class T> 
void SortedListClass< T >::clear()
{
    LinkedNodeClass< T >*tempHead;
    LinkedNodeClass< T >*tempTail;
    tempHead = head; 
    tempTail = tail;

    while(tempHead != NULL || tempTail != NULL)
    {
        cout << "Deleting node(s) --> "<< endl;  

        tempHead = head;
        delete tempHead;
        tempHead = NULL; 
        head = tempHead;

        tempTail = tail;
        delete tempTail;
        tempTail = NULL;
        tail = tempTail;

    } 
    cout<<"Deleting complete"<<endl;       
}

project5.cpp

int main()
{

    SortedListClass< int >testList;
    testList.insertValue(3);
    testList.printForward();
    testList.insertValue(4);
    testList.printForward();
    int theVal = 0;
    SortedListClass<int>copyTestList(testList);
    copyTestList = testList;
    copyTestList.getNumElems();
    int index = 0; 
    int outval = 0;
    copyTestList.getElemAtIndex(index,outval);
    copyTestList.printForward();
    copyTestList.removeLast(theVal);
    copyTestList.printBackward();
    return 0 ;
  }
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Leon
  • 5
  • 1
  • The shown code fails to meet stackoverflow.com's requirements for a [mcve], so no authoritative answer will be possible; so the general answer "the segmentation fault is because of a bug somewhere in your code" would apply. Still, it seems that the default constructor initializes some class members called `head` and `tail`, which are nowhere to be found in the copy constructor. That seems likely to be very, very wrong. Then, in clear(): `tempHead = NULL; head = tempHead;` seems to be quite wrong too. There are too many obvious, fundamental problems here. – Sam Varshavchik Nov 29 '17 at 00:56
  • Your `clear` function makes no sense at all. At the end of the first iteration of the `while` loop, `head` and `tail` are both set to `NULL`. So how can it ever loop? I agree with Sam's comment, "*There are too many obvious, fundamental problems here.*" You just have to find/fix *all* the bugs and maybe this project is too ambitious for your skill level. – David Schwartz Nov 29 '17 at 00:59
  • The copy constructor not initializing the `head` and `tail` members, and the `while()` loop looks all wrong. And the `operator=` looks like it would cause an endless recursive loop. – Remy Lebeau Nov 29 '17 at 01:05
  • Unrelated: `template void SortedListClass< T >::operator=(const SortedListClass< T > &rhs) { this->clear(); *this = SortedListClass(rhs); }` is doomsville. `*this = SortedListClass(rhs);` will result in infinite recursion. Give https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom a read for a better path. – user4581301 Nov 29 '17 at 01:06
  • What is the type of `head` and `tail`? Is this able to compile without declaring the type? – Elfen Dew Nov 29 '17 at 01:07

1 Answers1

0

The code you have shown for SortedListClass.inl is incomplete, but the pieces you have shown are all wrong. Try something more like this instead:

template<class T>
SortedListClass<T>::SortedListClass()
    : head(NULL), tail(NULL) /*, init other members as needed ... */
{
     cout << "Empty list initialized...." << endl; 
}

template<class T>
SortedListClass<T>::SortedListClass(const SortedListClass<T> &rhs)
    : head(NULL), tail(NULL) /*, init other members as needed ... */
{
    cout << "Copying value(s)" << endl;  

    LinkedNodeClass<T> *rhsNode = rhs.head;
    while (rhsNode)
    {
        insertValue(rhsNode->getValue());
        rhsNode = rhsNode->getNext();
    } 

    cout << "Copying complete" << endl;
}

template<class T> 
SortedListClass<T>::~SortedListClass()
{
    clear();
}

template<class T> 
SortedListClass<T>& SortedListClass<T>::operator=(const SortedListClass<T> &rhs)
{
    if (&rhs != this)
    {
        SortedListClass<T> tempList(rhs);
        std::swap(head, tempList.head);
        std::swap(tail, tempList.tail);
        // swap other members as needed ...
    }

    return *this;
}

template<class T> 
void SortedListClass<T>::clear()
{
    cout << "Deleting node(s)" << endl;  

    LinkedNodeClass<T> *tempNode = head; 

    head = NULL; 
    tail = NULL; 
    // reset other members as needed ...

    while (tempNode)
    {
        LinkedNodeClass<T> *tempNext = tempNode->getNext();
        delete tempNode;
        tempNode = tempNext;    
    } 

    cout << "Deleting complete" << endl;
}

template<class T> 
void SortedListClass<T>::insertValue(const T &value)
{
    LinkedNodeClass<T> *newNode = new LinkedNodeClass<T>(tail, value, NULL);

    if (!head)
        head = newNode;

    if (tail)
        tail->setNext(newNode);
    tail = newNode;

    // increment size counter, if applicable...
}

template<class T> 
void SortedListClass<T>::removeLast(const T &value)
{
    LinkedNodeClass<T> *tempNode = tail;
    while (tempNode)
    {
        LinkedNodeClass<T> *tempPrevious = tempNode->getPrevious();

        if (tempNode->getValue() == value)
        {
            LinkedNodeClass<T> *tempNext = tempNode->getNext();

            if (tempPrevious)
                tempPrevious->setNext(tempNext);

            if (tempNext)
                tempNext->setPrevious(tempPrevious);

            if (tempNode == tail)
                tail = tempPrevious;

            if (tempNode == head)
                head = tempNext;

            delete tempNode;

            // decrement size counter, if applicable...

            return;
        }

        tempNode = tempPrevious;
    } 
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770