473,226 Members | 1,376 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,226 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 4352

"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...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.