473,785 Members | 2,736 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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


Jul 22 '05 #1
1 4410

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

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

Similar topics

18
2430
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 description of how each works. I've been trying Google for the last 20 minutes but can't get anything decent. Thanks.
0
1230
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 type std:string): ------
1
1851
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 white spaces (global variable mLineStr is of type std::string, and the program has just the main thread):
1
5241
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 the class might grow later and I prefer having my copy constructor. This also explains why I have 2 similar function "ClearAll() and NullAll()" they might be used later in case I have pointers.
10
5083
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), c.end()); // this is the best way to get rid of objects where badValue returns true when c is a vector, string, or dequeue c.remove_if(badValue); // this is the best way to get rid of objects where badValue returns true when c is a list
11
1777
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
904
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", "passwort" from "users" order by "users"
2
3004
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
2347
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 because the value of j-str.length comes out to be some junk value int j = 12; std::string
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10087
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9947
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7496
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.