Question about linked-list copy ctor

This is a discussion on Question about linked-list copy ctor within the C++ Programming forums, part of the General Programming Boards category; What is the typical way of handling list copying? Do you normally clear() and then swap(const List& rhs)? What I ...

  1. #1
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391

    Question about linked-list copy ctor

    What is the typical way of handling list copying?

    Do you normally clear() and then swap(const List& rhs)?

    What I don't understand about that is clear() should effectively delete all the nodes in the list, but what happens with swap() after clear()? Does the rhs list receive a list that doesn't exist?

    My first thought then is to handle it in this manner:
    Code:
    template <typename T>
    List<T>::List(const List& rhs_l) : head(0), tail(0), sizeOfList(0){
    
        clear();
    
        for(Iter i(rhs_l.head); i != end(); i++){
    
            push_back(*i);
    
        }
    }
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    This is the copy constructor. This creates a completely fresh object - what is there to clear?

    Edit: you are probably confusing copy assignment (the assignment operator) and copy constructing.
    Last edited by anon; 03-06-2008 at 09:25 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Thanks anon,

    so this is the correct copy ctor, but I should get rid of clear()?
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Your copy routine is not correct (or at least it looks suspicious). Use a bog-standard iterator loop for that.

    And yes, get rid of the clear.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Errr, I'm getting a seg fault when I call the copy ctor like this:

    Code:
    List<int> m_list2( m_list );
    That's with or without the use of clear() in the copy ctor:
    Code:
    template <typename T>
    
    List<T>::List(const List& rhs_l) : head(0), tail(0), sizeOfList(0){
    
        
    
        //clear();
    
        
    
        for(Iter i(rhs_l.head); i != end(); i++){
    
            
    
            push_back(*i);
    
        }
    
    }
    should head and tail be initialized in some other way?

    lf I change the constructor to this:
    Code:
    template <typename T>
    
    List<T>::List(const List& rhs_l) : sizeOfList(0){
    
    	
    
    	head = tail = new Node(NULL);
    
        
    
        for(Iter i(rhs_l.head); i != end(); i++){
    
            
    
            push_back(*i);
    
        }
    
    }
    I don't get the seg fault but of course now I have an erroneous node in the list that I don't want.

    What's gong on here?
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  6. #6
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Quote Originally Posted by CornedBee View Post
    Use a bog-standard iterator loop for that.
    a bog-standard iterator?
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Perhaps this?
    Code:
       for(Iter i(rhs_l.head); i != rhs_l.end(); i++){
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    As I said, your loop looks suspicious.

    But it could also be that push_back tries to set the prev pointer on the tail node.


    Bog-standard iterator loop.
    Code:
    for(whatever::iterator it = rhs.begin(); it != rhs.end(); ++it)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Quote Originally Posted by matsp View Post
    Perhaps this?
    Code:
       for(Iter i(rhs_l.head); i != rhs_l.end(); i++){
    --
    Mats
    Yea, that fixed it. I had previously changed end() to return null (not tail->next) so I didn't need to prefix it with rhs, but that was all a stupid mistake.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 09:55 PM
  2. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  3. Linked list copy constructor issue
    By Craptastic! in forum C++ Programming
    Replies: 1
    Last Post: 08-03-2003, 09:30 PM
  4. How to use Linked List?
    By MKashlev in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 08:11 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21