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 1 4290
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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),...
|
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);
|
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",...
|
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
|
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...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
|
by: Ricardo de Mila |
last post by:
Dear people, good afternoon...
I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control.
Than I need to discover what...
|
by: Johno34 |
last post by:
I have this click event on my form. It speaks to a Datasheet Subform
Private Sub Command260_Click()
Dim r As DAO.Recordset
Set r = Form_frmABCD.Form.RecordsetClone
r.MoveFirst
Do
If...
|
by: jack2019x |
last post by:
hello, Is there code or static lib for hook swapchain present?
I wanna hook dxgi swapchain present for dx11 and dx9.
|
by: DizelArs |
last post by:
Hi all)
Faced with a problem, element.click() event doesn't work in Safari browser.
Tried various tricks like emulating touch event through a function:
let clickEvent = new Event('click', {...
| |