Thread: Copy Constructor crashing program

  1. #1
    Registered User
    Join Date
    Apr 2002

    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:

    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;
                        temp2->next = start;
         	    last->next = temp2;
         	    (temp2->info).key = (temp1->info).key;
         	    last = temp2;
    	    temp1 = temp1->next;
    Here's how I invoke the copy constructor:

    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
    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
    Here is the class header file:

    #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(const listType &); // copy constructor
            void remove(nodeptr loc, nodeptr preloc);
            void insert(nodeptr loc, nodeptr preloc, keytype key);
             nodeptr     start;
             int	           len;				
    Hope this helps. Thanks
    Last edited by bob2509; 11-12-2002 at 11:26 AM.

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


  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Göteborg, Sweden
    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
    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, 10:58 PM
  2. Making a program copy itself
    By kzar in forum C Programming
    Replies: 6
    Last Post: 05-29-2006, 03:13 AM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  4. Simple copy program hangs?
    By curlious in forum Linux Programming
    Replies: 3
    Last Post: 07-24-2004, 05:00 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM
Website Security Test