Thread: Healty iteration of string in C

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    110

    Healty iteration of string in C

    I have a function in which I iterate through strings. It seem fairly ok, but sometimes I run into segmentation fault. Is there a way of iterating in C++ that's safer?..
    Code:
    for(int i = offset; i < (str.size()/4); i = i+4){
    ..		// do stuff at every fourth character, index(4*i);
    ..	..}
    Or is there a better way of indexing i?

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    std::string has iterator types of various flavours which you might explore. for your example of every fourth character you might consider using modulus of i
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    110
    and that would be i%4 in C++?

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Code:
    for(int i = offset; i < (str.size()/4); i = i+4)
    Why are you dividing str.size() by 4? Removing that division will make it work as expected.
    Code:
    for( int i=offset; i<str.size(); i+=4 )
    You can replace i = i+4 by i+=4, as a shorter hand notation which accomplishes the same thing, if you want.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    110
    Thanks for the input! It turns out I have, or had, some gnarly elsewhere.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Imagine a three character string, "abc".
    i = 0, so i < str.size() (= 3). The loop body will execute and assume there are 4 characters, reading past the end of the buffer. Instead, you'll want:
    Code:
    for(int i = offset; i < str.size() - 3; i += 4)
    Or something similar. But even with this you have to be careful: str.size() is unsigned, to str.size() - 3 may underflow if str.size() < 3. So either catch that exception, or use something along the lines of:
    Code:
    for(int i = offset; i + 3 < str.size(); i += 4)

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Imagine a three character string, "abc".
    i = 0, so i < str.size() (= 3). The loop body will execute and assume there are 4 characters, reading past the end of the buffer. Instead, you'll want:
    No problems here, the normal progression of the loop would be
    i = 0, i < 3, repeat.
    i = 4, i < 3, stop.
    Even if there were a situation where i = 3, 3 is not < 3 so the loop stops anyway.

  8. #8
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    Quote Originally Posted by überfuzz View Post
    I have a function in which I iterate through strings. It seem fairly ok, but sometimes I run into segmentation fault. Is there a way of iterating in C++ that's safer?..
    Code:
    for(int i = offset; i < (str.size()/4); i = i+4){
    ..		// do stuff at every fourth character, index(4*i);
    ..	..}
    Or is there a better way of indexing i?
    As twomers indicated, you can use +4 instead of dividing the str.size() by 4. Make sure that you examine the length of string before your loop to eliminate the cases where str.size()<4.

    What is the "stuff" you are doing in your loop. Are you sure that the SIGSEGV is because of the for loop or some other assignment/modification in the loop

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I was going to say, you haven't shown the code where the real problem is.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by whiteflags View Post
    No problems here, the normal progression of the loop would be
    i = 0, i < 3, repeat.
    i = 4, i < 3, stop.
    Even if there were a situation where i = 3, 3 is not < 3 so the loop stops anyway.
    Ah, I misread the OP. I thought he acted upon groups of 4 characters, ie. in the first loop iteration read/write the character with indices 0 through 3. I see now that there is little reason to assume this is the case.

  11. #11
    Registered User
    Join Date
    Mar 2012
    Posts
    110
    I read about iteration an it seems an iterator should do the trick. This is what I ended up with.

    Code:
    	string compstr;
    	string::iterator it;
    	int index = offset;
    
    ..for ( it = str.begin() ; it < str.end(); ++it){
    		if (index%4 == 0){
    ..  		compstr += (char)map.find(str.substr(index,4))->second;
       	}
      		index++;
       }
    
    	return compstr;
    Don't hesitate to comment if I've got it wrong in some way.

    As I wrote in my last post, there's still popping up segmentation fault. It seems to be some issue in a file to string function I use. If I hard code the string directly in my code everything is fine and dandy. But if I use my file2string function I sometimes get segfault.

  12. #12
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    Quote Originally Posted by überfuzz View Post
    .....
    As I wrote in my last post, there's still popping up segmentation fault. It seems to be some issue in a file to string function I use. If I hard code the string directly in my code everything is fine and dandy. But if I use my file2string function I sometimes get segfault.
    Well, without having file2string code, all the commets will be nothing but a stab in the dark.

    I would try running the code in a debugger and examine the code snippet where SIGSEGV occurs.

  13. #13
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by überfuzz View Post
    As I wrote in my last post, there's still popping up segmentation fault. It seems to be some issue in a file to string function I use. If I hard code the string directly in my code everything is fine and dandy. But if I use my file2string function I sometimes get segfault.
    One possibility is that map.find(...) returns map.end(). map.end()->second is not valid.


    Quote Originally Posted by EVOEx View Post
    Ah, I misread the OP. I thought he acted upon groups of 4 characters, ie. in the first loop iteration read/write the character with indices 0 through 3. I see now that there is little reason to assume this is the case.
    Actually, I take that back. Apparently he *was* acting on 4 characters at a time. Though to be fair, not in any way that would overflow.

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The cast and the less-than comparison with the iterator looks dodgy to me. I'd remove the cast and use !=
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic String Iteration Help, Best Solution?
    By Shokwav in forum C++ Programming
    Replies: 5
    Last Post: 08-02-2012, 03:34 PM
  2. Iteration of loop
    By funky in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2009, 07:26 AM
  3. Iteration?
    By Aliaks in forum C++ Programming
    Replies: 2
    Last Post: 06-06-2009, 11:17 PM
  4. Iteration
    By juststartedC in forum C Programming
    Replies: 3
    Last Post: 11-18-2007, 08:00 AM
  5. Iteration help please
    By incognito in forum C++ Programming
    Replies: 3
    Last Post: 12-09-2001, 07:37 AM