Linked list copy constructor issue

This is a discussion on Linked list copy constructor issue within the C++ Programming forums, part of the General Programming Boards category; EDIT - *slaps forehead* A copy constructor is not the same as overloading the = sign...is it? /END EDIT I'm ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    1

    Linked list copy constructor issue

    EDIT - *slaps forehead*
    A copy constructor is not the same as overloading the = sign...is it?
    /END EDIT
    I'm currently coding a program that lets me calculate 2 very large integer values using linked lists. At the moment, I'm just making sure that the numbers are stored properly before overloading any operators.

    The problem is, I created a Temp variable and made it equal to the first number, but when the program exits and deletes the linked lists, the verbose data shown in the destructor for the Temp var is basically garbage. Especially in a situation where I enter number with more than 3 digits, in which it enters an infinite loop where it "deletes" a pointer containing the same value.

    It seems that when it deletes the linked list containing the first number, the Temp variable gets affected. This is is strange since that implies that the Temp node pointers are sharing addresses with the first number. So with this in mind, I've concluded that it is an issue with my copy constructor, but I fail to see how it is sharing memory with the item it is copying.

    Code:
    Number::Number(const Number &ListToCopy)
    { //seems to share memory locations with listtocopy and this causes big issues when deleting
        IntNodePtr Temp = ListToCopy.Head; 
        
        headInsert(Head, Temp->getData()); //copies data from first node and creates a copy node
        for (IntNodePtr Temp = ListToCopy.Head; Temp != NULL; Temp = Temp->getLink())
        {
            Temp = Temp->getLink();
            insert(Head, Temp->getData()); //should create a new node with copied data    
        }
        Negative = ListToCopy.Negative;
    }
    Any help would be appreciated.
    Last edited by Craptastic!; 08-03-2003 at 08:29 PM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >    for (IntNodePtr Temp = ListToCopy.Head; Temp != NULL; Temp = Temp->getLink())
    >    {
    >        Temp = Temp->getLink();
    You are doing Temp = Temp->getlink() twice, once in the for-loop statement, and again within the for-loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list question
    By mikeman in forum C Programming
    Replies: 1
    Last Post: 11-30-2008, 01:56 PM
  2. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-28-2008, 12:24 AM
  3. Replies: 5
    Last Post: 11-04-2006, 06:39 PM
  4. singly linked to doubly linked
    By jsbeckton in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 07:47 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

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