473,379 Members | 1,184 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,379 software developers and data experts.

Problem with std::string erase function


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: 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 for any help,

Tero
Jul 22 '05 #1
1 1828

"Tero Toivanen" <dr****@yahoo.com> wrote in message
news:41***************@yahoo.com...

Dear experts,


This isn't a chat room, you don't have to keep posting the same message.

Wait a little while and you will get some replies.

FWIW I couldn't see anything wrong with your code.

John
Jul 22 '05 #2

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

Similar topics

11
by: Christopher Benson-Manica | last post by:
Let's say I have a std::string, and I want to replace all the ',' characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the following the best way to do it? int idx; while(...
1
by: Chris Mantoulidis | last post by:
PROBLEM: I'm having some weird problems with string::find() (in ParamGenerate()), but since I'm not sure if that is the source of all bad output in my program, I'm posting least code that's...
4
by: Christopher | last post by:
I am using std::string to parse a command given by the user, I don't understand why the following snippet is not working as expected. string buffer, // store commands from...
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...
1
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...
22
by: Jason Heyes | last post by:
Does this function need to call eof after the while-loop to be correct? bool read_file(std::string name, std::string &s) { std::ifstream in(name.c_str()); if (!in.is_open()) return false; ...
8
by: vidya.bhagwath | last post by:
Hello Experts, I am using std::string object as a member variable in one of the my class. The same class member function operates on the std::string object and it appends some string to that...
13
by: arnuld | last post by:
/* C++ Primer 4/e * section 3.2 - String Standard Library * exercise 3.10 * STATEMENT * write a programme to strip the punctation from the string. */ #include <iostream> #include...
25
by: Bala2508 | last post by:
Hi, I have a C++ application that extensively uses std::string and std::ostringstream in somewhat similar manner as below std::string msgHeader; msgHeader = "<"; msgHeader += a; msgHeader...
1
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...
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
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.