1

I am in the process of creating my own linked list. I am attempting to create an overloaded constructor of my linked list that takes two node_iterators that point to the head and tail of another linked list.

overloaded list constructor:

template <typename T>
list<T>::list(node_iterator<T> front, node_iterator<T> back)
{
    unsigned temp;
    this->head = front.get_current();
    while(front != back)
    {
        ++temp;
        ++front;
    }
    this->tail = back.get_current();
    this->used = temp;

}

Above head and tail are node<T>* to my nodes in the list. front and back node_iterators that I have created. The get_current() method returns a node<T>* to the current node my node_iterator class is pointing at. I have tested my overloaded ++ and != operator in my node_iterator class and they work. However when attempt to create my list class with the above constructor I get the following error:

*** glibc detected *** ./a.out: munmap_chunk(): invalid pointer: 0xbf91b1a4 ***

Any tips on how I can fix this or do the above in a better fashion? Please let me know if you need to see more.

8
  • Why would you ever need a constructor that takes the head and tail of another list? Those values should be private members of the class and inaccessible from the calling scope anyway. Commented Mar 9, 2012 at 5:51
  • 1
    For a start, temp is never initialised. Commented Mar 9, 2012 at 5:52
  • I think a straight-up debug session would tell you more than any guess work given here. Have you stepped through with a debugger? Which line does the crash occur on? As an aside, temp is uninitialized (it's not the crasher - at least not here). Commented Mar 9, 2012 at 5:55
  • @JonathonReinhart my node iterators are a separate class and are not members of my linked list class. Commented Mar 9, 2012 at 5:56
  • Also, I'm curious - are the nodes used in this constructor going to live on both lists? How will the list destructor deal with that? Commented Mar 9, 2012 at 5:58

1 Answer 1

1

temp is never initialised. I guess it should have been

unsigned temp = 0;

As a result of the missing = 0, this->used ends up being some random value. I guess some time later in the code (not actually in the constructor), this causes the crash.

Sign up to request clarification or add additional context in comments.

1 Comment

This was one of many of my problems. Thank you for catching it.

Your Answer

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