Thread: error with strings...

  1. #1
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003
    Posts
    63

    error with strings...

    I'm am working on a string class. Each character of the string occupies one node. The function that my string class uses to convert from char * to string (the name of my string class) is giving me problems. Because my code is fairly short still, I will post all of it:

    Code:
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    
    class string
    {
    protected:
    	struct node
    	{
    		node * next;
    		char c;
    		node() : next(0) {}
    	};
    	node * head;
    	node * current;
    public:
    	string();
    	string(char *);
    	string & operator=(string &);
    };
    string::string()
    {
    	head = new node;
    	current = head;
    }
    string::string(char * str)
    {
    	current = head;
    	unsigned int a = 0;
    	while(str[a] != '\0')
    	{
    		if (current == 0)
    			current = new node;
    		/*the problem*/current->c = str[a];
    		cout << current->c << endl;
    		a++;
    		current = current->next;
    	}
    }
    string & string::operator=(string & s)
    {
    	s.current = s.head;
    	current = head;
    	while (s.current != 0)
    	{
    		if (current == 0)
    			current = new node;
    		current->c = s.current->c;
    		s.current = s.current->next;
    		current = current->next;
    	}
    	if (current->next != 0)
    	{
    		current = current->next;
    		while (current != 0)
    		{
    			delete current;
    			current = current->next;
    		}
    	}
    }
    
    int main()
    {
    	string test = "fart";
    	return 0;
    }
    The problem occurs on, guess where, the line that has the comment "the problem". I have no idea why I get an unhandled exception message because of this line. Any help anyone can give would be very much appreciated.
    Last edited by Mr_Jack; 03-08-2004 at 05:00 PM.

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    what is c? I'm assuming a string.
    is it in the same class?

    edit: why are you defining current twice?
    can you post your class interface?

    edit2: I see you edited it as I posted mine
    Last edited by axon; 03-08-2004 at 04:59 PM.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003
    Posts
    63
    sorry, I clicked submit by mistake and my incomplete message was posted.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Each character of the string occupies one node.
    That's a bit wasteful, don't you think?

    >I have no idea why I get an unhandled exception message.
    How about some more code. For example, something we can compile to get the error. I would wager that you only create a new node when current is null, but since you never set current->next to be null you access an uninitialized pointer.

    [edit]
    Your linked list code is flawed. Stripping out the unnecessaries, this would be a good start:
    Code:
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    
    class string
    {
    protected:
      struct node
      {
        node * next;
        char c;
        node() : next(0) {}
      };
      node * head;
      node * current;
    public:
      string();
      string(char *);
    };
    string::string()
    {
      head = new node;
      current = head;
    }
    string::string(char * str)
    {
      current = head = 0;
      unsigned int a = 0;
      while(str[a] != '\0')
      {
        if (current == 0) {
          current = new node;
          current->next = 0;
        }
        current->c = str[a];
        cout << current->c << endl;
        a++;
        current = current->next;
      }
    }
    
    int main()
    {
      string test = "fart";
      return 0;
    }
    [/edit]
    Last edited by Prelude; 03-08-2004 at 05:04 PM.
    My best code is written with the delete key.

  5. #5
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    well I compiled your code, and that particular line didn't give me any errors. The error exists here:
    Code:
    string & string::operator=(string & s)
    {
    	s.current = s.head;
    	current = head;
    	while (s.current != 0)
    	{
    		if (current == 0)
    			current = new node;
    		current->c = s.current->c;
    		s.current = s.current->next;
    		current = current->next;
    	}
    	if (current->next != 0)
    	{
    		current = current->next;
    		while (current != 0)
    		{
    			delete current;
    			current = current->next;
    		}
    	}
    }
    and this function has to return a value, and it doesn't

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  6. #6
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003
    Posts
    63
    I do feel stuipid. Thank you very much for helping me, it would have taken me a long time to figure the out the error with not initializing head and current. I probably would have figured out the deal without returning anything in my operator=() function because I do that all the time. Thanks to all for you help!

    prelude - what else do you suggest other than making each character have it's own node?

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >what else do you suggest other than making each character have it's own node?
    A dynamic array. That way each character doesn't have hidden overhead, and you can use standard string handling functions instead of having to roll your own specialized list processing routines. As it is, having a single character plus a pointer (which is usually the equivalent of 2 or 4 characters in size), increases the memory requirements of your strings considerably.
    My best code is written with the delete key.

  8. #8
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003
    Posts
    63
    could you give me an example of a dynamic array please?

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >could you give me an example of a dynamic array please?
    Code:
    char *str = new char[50];
    Now you have a dynamic array of 50 characters to work with. Don't forget to delete [] str; when you're done with it.
    My best code is written with the delete key.

  10. #10
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003
    Posts
    63
    Ok, thanks a lot for all of your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM