Thread: Difference between these two codes

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    110

    Difference between these two codes

    So I'm following my C++ textbook and its asking me why the top one doesn't work but the bottom does. I see that string s is defined outside in one, and inside of the while loop in the other. When running the first one, I get an error
    dealing with allocation size. These two codes are only portions of the original code, but the only difference is where string s is defined. I don't see why the top one doesn't work though. Can someone help me explain?

    "bordered" and "words" are two vectors of strings.
    "maxlen" is the length of the longest word in "words"

    Code:
                    vector<string> ret;
    
    	string::size_type width1 = maxlen + 1;
    
    	vector<string>::size_type i = 0, j = 0;
    	
    	string s;
    
    	while (i != words.size() || j != bordered.size()){
    
    		if(i != words.size()){
    			s = words[i++];
    		}
    
    		s += string(width1 - s.size(), ' ');
    
    		if (j != bordered.size()){
    			s += bordered[j++];
    		}
    
    		ret.push_back(s);
    	}
    and

    Code:
    vector<string> ret;
    
    	string::size_type width1 = maxlen + 1;
    
    	vector<string>::size_type i = 0, j = 0;
    	
    
    	while (i != words.size() || j != bordered.size()){
    		string s;
    
    		if(i != words.size()){
    			s = words[i++];
    		}
    
    		s += string(width1 - s.size(), ' ');
    
    		if (j != bordered.size()){
    			s += bordered[j++];
    		}
    
    		ret.push_back(s);
    	}

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    So what is the difference between using a new string each time, or keeping around the same one. Take note that the loop assigns once to s (but not necessarily) and then appends.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    Hm..from what I think, the top loop keeps the same string around, while the second makes a new instance everytime. I would assume the first loop would just keep adding more characters to the previous string. On the other hand, the second creates a new instance, so the previous string has no effect on th next. I do not see why the first loop would return an error though. I would think it would return a different vector of strings.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    s += string(width1 - s.size(), ' ');
    If words.size() < bordered.size(), what will happen to s?

    What will happen if s.size() > width1?

    Besides, you don't need to declare a new string each time, you can clear() the existing one.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    I can see that if s.size() is greater than width1, a negative amount of spaces would be appended to string s, which I assume returns an error?

    As for the words.size() being less than bordered.size(), I can't see the problem..

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I can see that if s.size() is greater than width1, a negative amount of spaces would be appended to string s, which I assume returns an error?
    Or rather a very large amount of spaces (seeing that these are unsigned values) which when appended to s might exceed the maximum length of a std::string?

    As for the words.size() being less than bordered.size(), I can't see the problem..
    s has a different value in the beginning of the iterations in these two codes. You really need to figure this out yourself. If necessary, "execute" the program on paper: make a table of relevant variables, see what each line of code does and update the values in the table. Or step through it with a debugger.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 01-22-2008, 09:58 AM
  2. Difference Equations / Recurrence Relations
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-05-2007, 10:26 AM
  3. What's the difference between var++ and ++var
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 05-31-2007, 02:27 AM
  4. : Difference between message queue and keyboard buffer...
    By Unregistered in forum Windows Programming
    Replies: 0
    Last Post: 02-21-2002, 03:47 PM