# Healty iteration of string in C

• 11-28-2012
überfuzz
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?
• 11-28-2012
rogster001
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
• 11-28-2012
überfuzz
and that would be i%4 in C++?
• 11-28-2012
twomers
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.
• 11-28-2012
überfuzz
Thanks for the input! It turns out I have, or had, some gnarly elsewhere.
• 11-28-2012
EVOEx
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)`
• 11-28-2012
whiteflags
Quote:

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.
• 11-28-2012
fnoyan
Quote:

Originally Posted by überfuzz
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
• 11-28-2012
iMalc
I was going to say, you haven't shown the code where the real problem is.
• 11-29-2012
EVOEx
Quote:

Originally Posted by whiteflags
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-29-2012
überfuzz
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.
• 11-29-2012
fnoyan
Quote:

Originally Posted by überfuzz
.....
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.
• 11-29-2012
EVOEx
Quote:

Originally Posted by überfuzz
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
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.
• 11-29-2012
iMalc
The cast and the less-than comparison with the iterator looks dodgy to me. I'd remove the cast and use !=