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

segmentation fault in following code

Why do i have segmentaion fault after executing str.erase(it.base()) ?
compiler gcc 3.3.6.

string TrimRight(const string& strin)
{
string str = strin;
string::reverse_iterator it = str.rbegin();
while(it!=str.rend() && *it==' '){
str.erase(it.base());
it = str.rbegin();
}
return str;
}
int main()
{

string str = "hello ";
cout << str << "|"<< endl;
str = TrimRight(str);
cout << str << "|"<< endl;

return 0;
}

Sep 1 '06 #1
4 2542
Denis Petronenko wrote:
Why do i have segmentaion fault after executing str.erase(it.base()) ?
compiler gcc 3.3.6.

string TrimRight(const string& strin)
{
string str = strin;
string::reverse_iterator it = str.rbegin();
while(it!=str.rend() && *it==' '){
str.erase(it.base());
it = str.rbegin();
}
return str;
}
IIRC, the base of rbegin() is end(). Then the first iteration of your
while loop is already a seg fault at erase.

Mark
Sep 1 '06 #2
Mark P schrieb:
IIRC, the base of rbegin() is end(). Then the first iteration of your
while loop is already a seg fault at erase.
Out of my head, wouldn't that render reverse iterators completely
useless? Pretty much every algorithm has to begin() somewhere and if
every *rbegin() is an autosegfault, this functionality would be
completely unusable.

IIRC, for an int a[10] there are following equivalences

a.begin() == &a[0];
a.end() == &a[10];
a.rbegin() == &a[9];
a.rend() ==&a[-1]

pls correct me if I'm wrong (other than iterators not really being
pointers).

Sep 1 '06 #3
F.J.K. schrieb:
IIRC, for an int a[10] there are following equivalences

a.begin() == &a[0];
a.end() == &a[10];
a.rbegin() == &a[9];
a.rend() ==&a[-1]

pls correct me if I'm wrong (other than iterators not really being
pointers).
Ok, I am wrong and I correct myself ;-)

A reverse iterator contains a normal iterator current, so that
a.begin() == a.rend().current
a.end() == a.rbegin().current
As I already wrote, this would segfault all over the place (as indeed
the OP experiences). As a "clever solution" the STL implements the
*reverse_iterator operator to return *(current-1). The
reverse_operator.base() function however returns current directly,
which is the reason why above code (which doesn't compile btw.)
segfaults. The solution is obvious, only use reverse_iterator.base()-1.
The above example in its corrected form:

#include <iostream>
#include <string>
using namespace std;
string TrimRight(const string& strin)
{
string str = strin;
string::reverse_iterator it = str.rbegin();
while(it!=str.rend() && *it==' '){
str.erase(it.base()-1);
it = str.rbegin();
}
return str;
}

int main()
{

string str = "hello ";
cout << str << "|"<< endl;
str = TrimRight(str);
cout << str << "|"<< endl;

return 0;
}

Sep 1 '06 #4
"Denis Petronenko" <pe********@gmail.comschrieb im Newsbeitrag
news:11**********************@m79g2000cwm.googlegr oups.com...
Why do i have segmentaion fault after executing str.erase(it.base()) ?
compiler gcc 3.3.6.

string TrimRight(const string& strin)
{
string str = strin;
string::reverse_iterator it = str.rbegin();
while(it!=str.rend() && *it==' '){
str.erase(it.base());
it = str.rbegin();
}
return str;
}
Wouldn't

return strin.substr(0, strin.find_last_not_of(" ") + 1);

be much easier? You could also remove tabs and other whitespace with a
single statement.

Heinz

Sep 1 '06 #5

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

Similar topics

1
by: Michael Sgier | last post by:
Hello i get the error: Segmentation fault. The debugger stopped at the last of the following lines: // This stores the texture array for each of the textures assigned to this model unsigned...
3
by: Anks | last post by:
i am unable to find why following code is giving segmentation fault.... way to produce seg fault: run the program... give input 12345678....enter any key except 'x'.... again give 12345678 as...
6
by: damian birchler | last post by:
If I run the following I get a segmentation fault: #define NAMELEN 15 #define NPERS 10 typedef struct pers { char name; int money; } pers_t;
3
by: Zheng Da | last post by:
Program received signal SIGSEGV, Segmentation fault. 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 (gdb) bt #0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 #1 0x40094c54 in malloc...
6
by: I_have_nothing | last post by:
Hi! I am new in C. I try to use dynamical allocation fuction malloc( ) and realloc( ). I found something strange. After several calling realloc( ), the malloc( ) will give me a Segmentation...
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
27
by: Paminu | last post by:
I have a wierd problem. In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is not printed! ...
3
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection...
8
by: Bryan | last post by:
Hello all. I'm fairly new to c++. I've written several programs using std::vectors, and they've always worked just fine. Until today. The following is a snippet of my code (sorry, can't...
3
by: jr.freester | last post by:
I have created to classes Matrix and System. System is made up of type matrix. ---------------------------------------------------------------------------------- class Matrix { private: int...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.