473,320 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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::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


Jul 22 '05 #1
1 4361

"Tero Toivanen" <dr****@yahoo.com> wrote in message news:41***************@yahoo.com...
| 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
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: JKop | last post by:
Can some-one please point me to a nice site that gives an exhaustive list of all the memberfunctions, membervariables, operators, etc. of the std::string class, along with an informative...
0
by: Tero Toivanen | last post by:
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...
1
by: Tero Toivanen | last post by:
Dear experts, I am doing code to Solaris 9 system with Forte 6 Update 2 C++ compiler.. I get every now and then segmentation fault in the following code that removes heading and trailing...
1
by: Simon | last post by:
Hi, I have a class that does not seem to work. I cannot see the problem, and the "fix" I have found does not help me understand what the problem was I know I don't need a copy constructor but...
10
by: Piotr | last post by:
In Effective STL item 9 "Choose carefully among erasing options", it has this example: bool badValue(int x); // returns whether x is 'bad' c.erase ( remove_if(c.begin(), c.end(), badValue),...
11
by: Gaijinco | last post by:
I did this function: void clean(string&s) { for(size_t i=0; i<s.size(); ++i) { if(isalpha(s)) s=tolower(s); else s.erase(i,1);
11
by: Lothar Behrens | last post by:
Hi, I have selected strtok to be used in my string replacement function. But I lost the last token, if there is one. This string would be replaced select "name", "vorname", "userid",...
2
by: peace357 | last post by:
I am trying to write two recursive functions involving two strings, 1)CAT & 2)MAN. My function needs to print out: TACMAN ATCMAN CTAMAN TCAMAN ACTMAN CATMAN
1
by: Pradeep | last post by:
Hi All, I am facing an issue where length method of std::string class gives a junk value when used in a expression. Here's an example. The code should not go into the for loop but it does...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.