Problem with std::string erase function.

T

Tero Toivanen

Dear experts,

I am doing code to Solaris 9 system with C++.

I get every now and then segmentation fault in the following code that
removes
heading and trailing white spaces (mLineStr is of type std:string):

------

1: void ClassXXX::FunctionYYY (const std::string & inCmd)
2: {
3: mLineStr = inCmd;
4:
5: int tmpSpaceNum = 0;
6: int tmpLength = mLineStr.length();
7:
8: // remove heading white spaces
9: for (int idx1=0; idx1 < tmpLength; idx1++)
10: {
11: if (isspace( mLineStr [idx1] ))
12: {
13: tmpSpaceNum++;
14: }
15: else
16: break;
17: }
18:
19: mLineStr.erase (0, tmpSpaceNum);
20:
21: tmpSpaceNum = 0;
22: tmpLength = mLineStr.length();
23:
24: // remove trailing whites paces
25: for (int idx2=tmpLength-1; idx2 >= 0; idx2--)
26: {
27: if (isspace( mLineStr [idx2] ))
28: {
29: tmpSpaceNum++;
30: }
31: else
32: break; // leave the loop
33:
34: }
35:
36: mLineStr.erase (tmpLength-tmpSpaceNum, tmpSpaceNum);

-------

Now with mLineStr = "foobar -d jomppe -m4 " there will be segmentation
fault
in the last line with a printing to stderr: "Position 20 is greater than
size 6" . This
segmentation fault does not come every time, but only every now and
then. I guess
the segmentation fault comes because an exception is not catched, but I
cannot
understand the reason for the exception!.

Is the memory somehow corrupted or is there some known bugs in erase
function?
After all, in line 22 the length of mLineStr still seems to be 21
characters, but in
line 36 it is for some reason 6...

Thank you very much in advance,

Tero
 
C

Chris \( Val \)

| Dear experts,
|
| I am doing code to Solaris 9 system with C++.
|
| I get every now and then segmentation fault in the following code that
| removes
| heading and trailing white spaces (mLineStr is of type std:string):
|
| ------
|
| 1: void ClassXXX::FunctionYYY (const std::string & inCmd)
| 2: {
| 3: mLineStr = inCmd;
| 4:
| 5: int tmpSpaceNum = 0;
| 6: int tmpLength = mLineStr.length();
| 7:
| 8: // remove heading white spaces
| 9: for (int idx1=0; idx1 < tmpLength; idx1++)
| 10: {
| 11: if (isspace( mLineStr [idx1] ))
| 12: {
| 13: tmpSpaceNum++;
| 14: }
| 15: else
| 16: break;
| 17: }
| 18:
| 19: mLineStr.erase (0, tmpSpaceNum);
| 20:
| 21: tmpSpaceNum = 0;
| 22: tmpLength = mLineStr.length();
| 23:
| 24: // remove trailing whites paces
| 25: for (int idx2=tmpLength-1; idx2 >= 0; idx2--)
| 26: {
| 27: if (isspace( mLineStr [idx2] ))
| 28: {
| 29: tmpSpaceNum++;
| 30: }
| 31: else
| 32: break; // leave the loop
| 33:
| 34: }
| 35:
| 36: mLineStr.erase (tmpLength-tmpSpaceNum, tmpSpaceNum);
|
| -------
|
| Now with mLineStr = "foobar -d jomppe -m4 " there will be segmentation
| fault
| in the last line with a printing to stderr: "Position 20 is greater than
| size 6" . This
| segmentation fault does not come every time, but only every now and
| then. I guess
| the segmentation fault comes because an exception is not catched, but I
| cannot
| understand the reason for the exception!.
|
| Is the memory somehow corrupted or is there some known bugs in erase
| function?
| After all, in line 22 the length of mLineStr still seems to be 21
| characters, but in
| line 36 it is for some reason 6...
|
| Thank you very much in advance,

I wrote myself a little function to do this sort of thing
in the past. Try this:

# include <iostream>
# include <ostream>
# include <string>

inline std::string TrimBS( const std::string& S,
const std::string& Delim = " \a\b\f\t\n\r\v" )
{
const std::string::size_type
First( S.find_first_not_of( Delim ) );

const std::string::size_type
Last( S.find_last_not_of( Delim ) );

return ( First == std::string::npos ) ?
std::string( "" ) :
S.substr( First, Last - First + 1 );
}

int main()
{
std::string Buffer( " \t\nExample Test String\n\t\r " );

std::cout << '*' << TrimBS( Buffer ) << '*' << std::endl;

return 0;
}

'TrimBS' returns an copy of the string, so as to preserve
the original, but you can easily change that if you wish.

Good luck.
Chris Val
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,823
Latest member
Nadia88

Latest Threads

Top