LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 08-15-2004, 04:42 AM   #1
kris273
LQ Newbie
 
Registered: Mar 2004
Posts: 19

Rep: Reputation: 0
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?
 
Old 08-15-2004, 09:37 AM   #2
raxxor
LQ Newbie
 
Registered: Jul 2003
Distribution: Fedora Core 2
Posts: 12

Rep: Reputation: 0
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
 
Old 08-15-2004, 09:54 AM   #3
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
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)
 
Old 08-15-2004, 02:59 PM   #4
kris273
LQ Newbie
 
Registered: Mar 2004
Posts: 19

Original Poster
Rep: Reputation: 0
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
 
Old 08-16-2004, 10:12 PM   #5
kris273
LQ Newbie
 
Registered: Mar 2004
Posts: 19

Original Poster
Rep: Reputation: 0
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.
 
Old 08-16-2004, 11:48 PM   #6
raxxor
LQ Newbie
 
Registered: Jul 2003
Distribution: Fedora Core 2
Posts: 12

Rep: Reputation: 0
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
 
Old 08-17-2004, 06:00 AM   #7
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
"Dynamic MMap ran out of room" error when adding new apt source list hasanito Debian 25 05-27-2013 03:56 AM
DPKG & "files list file for package `pkg-config' contains empty filename" error :( Angelus Debian 2 04-02-2005 07:34 AM
User "list" running process "python" TroelsSmit Linux - Newbie 2 02-22-2005 04:55 AM
smbclient "Error returning browse list: NT_STATUS_OK" b0nes Linux - Networking 2 02-18-2005 07:13 AM
YOU Fails With "Error: Could not save server list to disk." snufferz Linux - Newbie 2 04-02-2004 05:22 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration