Copy Constructor crashing program

This is a discussion on Copy Constructor crashing program within the C++ Programming forums, part of the General Programming Boards category; Hi All, Need some help here with a copy constructor. I'm trying to copy a character array instance. Here's what ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    47

    Copy Constructor crashing program

    Hi All,

    Need some help here with a copy constructor. I'm trying to copy a character array instance. Here's what i have so far:

    Code:
    listType::listType(const listType & init)	// Copy constructor
    {
    	nodeptr temp1, temp2, last;
    
    	start = NULL;
    	len = 0;
    
    	temp1 = init.start;
    	
    	if (temp1 != NULL)
                   {
        	    start = new ListNodeType;
    	    len = 1;
    	    start->next = start;
    	    (start->info).key = (temp1->info).key;
    	    last = start;
    	    temp1 = temp1->next;			
                    }
    	while (temp1 != NULL)
                   {
         	    temp2 = new ListNodeType;
         	    len++;
                        temp2->next = start;
         	    last->next = temp2;
         	    (temp2->info).key = (temp1->info).key;
         	    last = temp2;
    	    temp1 = temp1->next;
                    }
    return;
    }
    Here's how I invoke the copy constructor:

    Code:
    listType list1(list);
    When the copy constructor is invoked my program crashes. Don't know why.

    Any help would be appreciated.

  2. #2
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Its hard to tell without knowing more about your list structure. Try narrowing it down, remove the 'if' block followed by the 'while' block - you might be able to pinpoint it a bit closer.
    Couldn't think of anything interesting, cool or funny - sorry.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    47
    Here is the class header file:

    Code:
    #ifndef LIST_H
    #define LIST_H
    
    typedef char      keytype;	
    
    struct infotype					
    {					
           keytype      key;				
    };
    
    struct ListNodeType
    {
           infotype	info;		
           ListNodeType*	next;			
    };									
    typedef  ListNodeType*  nodeptr;
    
    class listType
    {
        public :
            listType();	
            listType(const listType &); // copy constructor
            ~listType();
            void remove(nodeptr loc, nodeptr preloc);
            void insert(nodeptr loc, nodeptr preloc, keytype key);
        private:
             nodeptr     start;
             int	           len;				
    };	
     #endif
    Hope this helps. Thanks
    Last edited by bob2509; 11-12-2002 at 12:26 PM.

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Add breakpoints in every few lines in the copy contructor and determine the line that crashes the program. Post that line.

    Kuphryn

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Are you sure that the original list is valid?
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    I'm still learning C++, so I'm not sure if it is possible to do this in a header file...listType(const listType &)...try using this though and see if it helps...

    replace listType(const listType &) with
    listType(const listType & init) in your header file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 11:58 PM
  2. Making a program copy itself
    By kzar in forum C Programming
    Replies: 6
    Last Post: 05-29-2006, 04:13 AM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  4. Simple copy program hangs?
    By curlious in forum Linux Programming
    Replies: 3
    Last Post: 07-24-2004, 06:00 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

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