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

Vector element erase cause SIGSEGV

Hi,

I don't know what's wrong with my program, it causes SIGSEGV:

#include <vector>

using namespace std;

int main(void)
{
vector<intv;
vector<int>::iterator iter;

int i,j,k;

i = 9;j = 10;k = 11;

v.push_back(i);
v.push_back(j);
v.push_back(k);

for (iter = v.begin();iter != v.end(); ++iter) {
printf("*********%d\n", *iter);
iter = v.erase(iter);
}

return 0;
}

Thanks.
ABAI

Jan 18 '07 #1
4 4385
Bin Chen wrote:
I don't know what's wrong with my program, it causes SIGSEGV:

#include <vector>

using namespace std;

int main(void)
{
vector<intv;
vector<int>::iterator iter;

int i,j,k;

i = 9;j = 10;k = 11;

v.push_back(i);
v.push_back(j);
v.push_back(k);

for (iter = v.begin();iter != v.end(); ++iter) {
Change the above line to:

for (iter = v.begin();iter != v.end();) {
printf("*********%d\n", *iter);
iter = v.erase(iter);
}

return 0;
}
std::vector::erase returns an iterator to the element immediately
following the one that was erased. So in effect, the expression "iter
= v.erase(iter)" causes iter to to be incremented. But your for loop
also increments iter. In other words, iter is incremented _twice_ each
time through the loop. The first time through the loop, iter begins
pointing to v[0] and ends pointing to v[2]. The second time through
the loop, iter begins pointing to v[2]. After printing the value of
v[2], the program erased v[2], and increments iter so that it is equal
to v.end(). Then, the program attempts to increment iter again (as
happens every time through your for loop), leading to undefined
behavior - in this instance, a seg fault.

To fix your program, change the line indicated above.

Best regards,

Tom

Jan 18 '07 #2

"Bin Chen дµÀ£º
"
Hi,

I don't know what's wrong with my program, it causes SIGSEGV:

#include <vector>

using namespace std;

int main(void)
{
vector<intv;
vector<int>::iterator iter;

int i,j,k;

i = 9;j = 10;k = 11;

v.push_back(i);
v.push_back(j);
v.push_back(k);

for (iter = v.begin();iter != v.end(); ++iter) {
printf("*********%d\n", *iter);
iter = v.erase(iter);
erase will return the next element after the iter. assuming you are
erasing the last element, then iter will be v.end (), and then in your
for loop you'll still ++iter, so it should be some errors.....
}

return 0;
}

Thanks.
ABAI
Jan 18 '07 #3
If the question can be reformulated as how to fix,
then this is something you need to do

int Size = v.size();
for (int I = 0; I < Size; I++) {
printf("*********%d\n", *v.begin());
v.erase(v.begin());
}
If you want really to know why it is not working,
this may give you a hint:
for (iter = v.begin();iter != v.end(); ++iter) {
printf("BEF *********%d\n", *iter);
iter = v.erase(iter);
printf("AFT *********%d\n", *iter);
}

Erasing any element except the last causes the vector to
be "rearanged", and return value is iterator to next element.
Erasing the last element seems not to return v.end() in my tests,
but returns the last element and this could be because the pointer
to the last location is in memory.

-haro
-----------------------------------------
Sorry if this is not complete answer, I just thought it
might help you quickly in case you are not trying to deeply
understand what is going on.
Bin Chen wrote:
Hi,

I don't know what's wrong with my program, it causes SIGSEGV:

#include <vector>

using namespace std;

int main(void)
{
vector<intv;
vector<int>::iterator iter;

int i,j,k;

i = 9;j = 10;k = 11;

v.push_back(i);
v.push_back(j);
v.push_back(k);

for (iter = v.begin();iter != v.end(); ++iter) {
printf("*********%d\n", *iter);
iter = v.erase(iter);
}

return 0;
}

Thanks.
ABAI
Jan 18 '07 #4
Thomas Tutone wrote:
>
for (iter = v.begin();iter != v.end();) {

printf("*********%d\n", *iter);
iter = v.erase(iter);
}
I would be tempted to write the loop this way:

for (iter = v.begin(); iter != v.end(); iter = v.erase(iter))
printf("*********%d\n", *iter);

Does anyone care to comment on whether or not this is good style?

--
Nate
Jan 19 '07 #5

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

Similar topics

1
by: Tino | last post by:
I have a std::vector<int> which, after some initialization, has a fixed number of elements...after initialization I must do the following repeatedly: I remove an element which could be anywhere in...
3
by: Jonathan | last post by:
Hey again everyone! I have another question for you guys. I am trying to erase a certain vector element based on what number the user selects. Here is what I did: case 'b' : { cout << "Please...
11
by: koperenkogel | last post by:
Dear cpp-ians, I am working with a vector of structures. vector <meta_segment> meta_segm (2421500); and the structure look like: struct meta_segment { float id; float num;
9
by: david wolf | last post by:
I want to delete all even numbers in a vector, I am not sure if there's any better way to do it. Following program is how I did it. Look at the part of code beginning from comments: //delete all...
5
by: Billy Patton | last post by:
I have a polygon loaded into a vector. I need to remove redundant points. Here is an example line segment that shows redundant points a---------b--------c--------d Both b and c are not...
3
by: Chad E. Dollins | last post by:
I would like to know how to remove an element from a vector. The following give a segmentation fault perhaps someone can give me a proper explanation why: vector<T> * myCon; //...some code that...
7
by: JH Programmer | last post by:
Hi, is there any ways that allow us to delete an element in between? say int_val: 1 int_val: 2 int_val: 3
3
by: =?iso-8859-1?q?Erik_Wikstr=F6m?= | last post by:
I have some code where there's this vector of pointers to objects and I need to delete and erase some of them, the problem is that to know which I need to iterate through the vector and I'm trying...
10
by: arnuld | last post by:
WANTED: /* C++ Primer - 4/e * * Exercise: 9.26 * STATEMENT * Using the following definition of ia, copy ia into a vector and into a list. Use the single iterator form of erase to...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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.