Bug in my copy constructor for a stack

This is a discussion on Bug in my copy constructor for a stack within the C++ Programming forums, part of the General Programming Boards category; Here is the copy constructor. Code: Stack::Stack(const Stack& a_stack) { if (a_stack.top != NULL) { StackFramePtr temp = a_stack.top; StackFramePtr ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    1

    Bug in my copy constructor for a stack

    Here is the copy constructor.

    Code:
        Stack::Stack(const Stack& a_stack)
        {
            if (a_stack.top != NULL)
            {
                StackFramePtr temp = a_stack.top;
                StackFramePtr end = new StackFrame;
    
                end->data = temp->data;
                top = end;
    
                temp = temp->link;
                while (temp != NULL)
                {
                    end = end->link;
                    end = new StackFrame;
    
                    end->data = temp->data;
                    temp = temp->link;
                }
                end->link = NULL;
            }
            else top = NULL;
        }
    The problem lies within these lines..
    Code:
                    end = end->link;
                    end = new StackFrame;
    The correct way to do this is given in the example..
    Code:
                    end->link = new StackFrame;
                    end = end->link;
    To me it seems like these two pairs of lines do the exact same thing, but in a different order. The order does not seem like it should make a difference but it does. When the first pair of lines is used in the copy constructor, it seems like it copies the first object from the stack that is being copied, then it goes on to create an infinite stack where each StackFrame object's data variable is assigned to random garbage. I do not believe this is some sort of loose pointer problem. Thanks for any help this is just something that I can't figure out on my own.

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by Jsel View Post
    To me it seems like these two pairs of lines do the exact same thing, but in a different order.
    One of them assigns to end->link, the other does not, how on Earth can they be the same?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    >>StackFramePtr temp = a_stack.top;
    FYI, this is called obfuscation. This would be better:
    StackFrame* temp = a_stack.top;

    >>StackFramePtr end = new StackFrame;
    This is dangerous. You should consider wrapping them in auto_ptr or unique_ptr to avoid memory leaks if an exception is thrown. It would also be better if you acquired all your memory at the start so that you don't leave your object in a zombie-state if new throws.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. When would you need to use a copy constructor
    By steve1_rm in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2008, 02:28 PM
  2. Copy Constructor
    By noobcpp in forum C++ Programming
    Replies: 3
    Last Post: 07-01-2007, 06:29 AM
  3. copy constructor
    By paperbox005 in forum C++ Programming
    Replies: 4
    Last Post: 08-20-2004, 01:43 PM
  4. copy constructor
    By Eber Kain in forum C++ Programming
    Replies: 1
    Last Post: 09-30-2002, 05:03 PM
  5. copy constructor
    By ygfperson in forum C++ Programming
    Replies: 6
    Last Post: 03-05-2002, 05:55 PM

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