Problem with std::string erase function.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tero Toivanen

    #1

    Problem with std::string erase function.

    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::Funct ionYYY (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




  • Chris \( Val \)

    #2
    Re: Problem with std::string erase function.


    "Tero Toivanen" <drei3t@yahoo.c om> wrote in message news:416E29BD.F 838F959@yahoo.c om...
    | 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::Funct ionYYY (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::si ze_type
    First( S.find_first_no t_of( Delim ) );

    const std::string::si ze_type
    Last( S.find_last_not _of( Delim ) );

    return ( First == std::string::np os ) ?
    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


    Comment

    Working...