Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
08-15-2004, 04:42 AM
|
#1
|
LQ Newbie
Registered: Mar 2004
Posts: 19
Rep:
|
an odd STL "List" error
I have the following code and I get a segmentation fault
Code:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <list> // list class library
using namespace std;
int main()
{
list<int> int_list;
int i;
for ( i = 0; i < 20; i++)
{
int_list.push_front(i*3);
}
list<int>::iterator int_iter;
for (int_iter = int_list.begin(); int_iter != int_list.end(); int_iter++ )
{
cout << *int_iter << " ";
}
cout << endl << "test1" << endl;
for (int_iter = int_list.begin(); int_iter != int_list.end(); int_iter++ )
{
if (*int_iter == 27)
{
int_list.erase(int_iter,(int_iter++));
}
}
cout << endl << "test2" << endl;
for (int_iter = int_list.begin(); int_iter != int_list.end(); int_iter++ )
{
cout << *int_iter << " ";
}
cout << endl;
}
compiling and excution:
Quote:
[list tests]$ g++ -o listtest2 listtest2.cpp [list tests]$ ./listtest2
57 54 51 48 45 42 39 36 33 30 27 24 21 18 15 12 9 6 3 0
test1
Segmentation fault
|
The STL erase command:
Quote:
iterator erase( iterator pos );
iterator erase( iterator start, iterator end );
|
When I take out the erase the program works, but it does nothing, is there a bug in the stl list... or am I just doing something amazingly stupid that I fail to see, or did I find a bad site or what?
|
|
|
08-15-2004, 09:37 AM
|
#2
|
LQ Newbie
Registered: Jul 2003
Distribution: Fedora Core 2
Posts: 12
Rep:
|
Hi,
You should use:
Code:
int_list.erase (int_iter--);
Or else your loop will try to go through all 20 items of the list, the only problem is there are only 19 now, and the additional int_iter++ for arg 2 of the erase will make it go for 21.
r
|
|
|
08-15-2004, 09:54 AM
|
#3
|
Senior Member
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263
Rep:
|
ok, the problem is with the erase as you assummed, you need to remember that end is one after the last element so erase(a,b) erases the range [a,b) that is from a to b excluding b
now you are calling erase(int_iter,(int_iter++)) which i reckon compiles to something like
temp=int_iter+1
erase(int_iter, int_iter)
int_iter=temp;
which isnt what you want as you can see. either of the following would work:
erase(int_iter++)
erase(int_iter,++int_iter)
|
|
|
08-15-2004, 02:59 PM
|
#4
|
LQ Newbie
Registered: Mar 2004
Posts: 19
Original Poster
Rep:
|
blah, I can't believe I left that in, if you take out the ++, it still doesn't work, that was me messing around with it and trying to figure out what's wrong
it was orginally "int_list.erase(int_iter);"
[Linux list tests]$ g++ -o listtest2 listtest2.cpp
[Linux list tests]$ ./listtest2
57 54 51 48 45 42 39 36 33 30 27 24 21 18 15 12 9 6 3 0
test1
Segmentation fault
|
|
|
08-16-2004, 10:12 PM
|
#5
|
LQ Newbie
Registered: Mar 2004
Posts: 19
Original Poster
Rep:
|
here's my code
Code:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <list> // list class library
using namespace std;
int main()
{
list<int> int_list;
int i;
for ( i = 0; i < 20; i++)
{
int_list.push_front(i*3);
}
list<int>::iterator int_iter;
for (int_iter = int_list.begin(); int_iter != int_list.end(); int_iter++ )
{
cout << *int_iter << " ";
}
cout << endl << "test1" << endl;
for (int_iter = int_list.begin(); int_iter != int_list.end(); int_iter++ )
{
if (*int_iter == 27)
{
cout << "it equals" << endl;
int_list.erase(int_iter);
}
}
cout << endl << "test2" << endl;
for (int_iter = int_list.begin(); int_iter != int_list.end(); int_iter++ )
{
cout << *int_iter << " ";
}
cout << endl;
}
here's the result
[Linux list tests]$ g++ -o listtest2 listtest2.cpp
[Linux list tests]$ ./listtest2
57 54 51 48 45 42 39 36 33 30 27 24 21 18 15 12 9 6 3 0
test1
it equals
Segmentation fault
[Linux list tests]$
Last edited by kris273; 08-16-2004 at 10:13 PM.
|
|
|
08-16-2004, 11:48 PM
|
#6
|
LQ Newbie
Registered: Jul 2003
Distribution: Fedora Core 2
Posts: 12
Rep:
|
Hi again,
Did you try using int_iter-- ? This mainly prevents another seg fault to be incase you delete the last item in the list... it also works for what you are doing:
Code:
~@thursday> ./test
57 54 51 48 45 42 39 36 33 30 27 24 21 18 15 12 9 6 3 0
test1
it equals
test2
57 54 51 48 45 42 39 36 33 30 24 21 18 15 12 9 6 3 0
~@thursday>
Just change int_list.erase(int_iter); to int_list.erase(int_iter--);
Hope this helps,
r
|
|
|
08-17-2004, 06:00 AM
|
#7
|
Senior Member
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263
Rep:
|
int_iter-- only works because list<> is doubally(sp?) linked, this wont work for a singly linked list. ie slist<>
the code i gave above "erase(int_iter++)" will work for both a singly and a doubally linked list. also there are no problems at the end because if you delete the last item int_iter++ will equal int_list.end() causing you to drop out of the loop before you try and dereference.
Last edited by kev82; 08-17-2004 at 06:02 AM.
|
|
|
All times are GMT -5. The time now is 06:24 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|