473,401 Members | 2,068 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,401 software developers and data experts.

Help - Segmentation Fault

1
Hi,

when I execute this code I receive a segmentation fault. I know that somethig's happening when I try to erase an element from the string list but I don't know a solution
The line which cause the erros is marked with a comment.
Any help is appreciated.

Thanks

Platform, OS, Version
i386, Linux, 2.6.5-7.191-bigsmp

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <sstream>
  3. #include <fstream>
  4. #include <cstring>
  5. #include <list>
  6.  
  7. using namespace std;
  8.  
  9. class ReadNC
  10. {
  11. public:
  12.     ReadNC();
  13.     ~ReadNC();
  14.     bool addNC(string);  // add a string in the string list
  15.     bool delNCEnt(string);  // delete a string from the string list
  16.     list<string> ncColl;
  17. };
  18.  
  19. ReadNC::ReadNC(){}
  20. ReadNC::~ReadNC(){}
  21.  
  22. bool ReadNC::addNC(string toAdd)
  23. {
  24.     bool rVal = false;
  25.     ncColl.push_back(toAdd);
  26.     return rVal;
  27. }
  28.  
  29. bool ReadNC::delNCEnt(string toDel)
  30. {
  31.     bool rVal=false;
  32.     list<string>::iterator it;
  33.     for (it=ncColl.begin(); it!=ncColl.end(); ++it){
  34.             string toComp=*it;
  35.         if (toComp.compare(toDel) == 0){
  36.             ncColl.erase(it);   //           ERROR!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  37.             rVal=true;
  38.         }        
  39.     }
  40.     return rVal;
  41. }
  42. int main()
  43. {
  44.         string a;
  45.     ReadNC nc;
  46.     cout<<nc.addNC("ABCD")<<endl;
  47.     cout<<nc.delNCEnt("ABCD")<<endl;
  48.     return 0;
  49. }
  50.  
Apr 16 '07 #1
1 1516
Ganon11
3,652 Expert 2GB
Whenever you use the list's erace method, the iterator you are using becomes invalid. You should either use the iterator's erase method (or remove(), or some such similar function) or find an alternate way to delete.
Apr 16 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: sivignon | last post by:
Hi, I'm writing a php script which deals with 3 ORACLE databases. This script is launch by a script shell on an linux machine like this : /../php/bin/php ./MySript.php (PHP 4.3.3) My script...
3
by: diyanat | last post by:
i am writing a cgi script in C using the CGIC library, the script fails to run, i am using apache on linux error report from apache : internal server error Premature end of script headers:...
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: I_have_nothing | last post by:
Hi! I am new in C. I got a lots of "Segmentation Fault"s in my code. I guess One possibility is: if " int array_i; " is declard and the code trys to access "array_i", a Segmentation Fault will...
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...
18
by: Digital Puer | last post by:
Hi, I'm coming over from Java to C++, so please bear with me. In C++, is there a way for me to use exceptions to catch segmentation faults (e.g. when I access a location off the end of an array)?...
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...
5
by: totoro2468 | last post by:
Here is a code I am writing. I keep geting segmentation fault. I'm not sure what i'm doing wrong. Can you explain it to me in PLAIN ENGLISH?? void ReadString (char *filename, int *lengthPtr,...
6
by: DanielJohnson | last post by:
int main() { printf("\n Hello World"); main; return 0; } This program terminate just after one loop while the second program goes on infinitely untill segmentation fault (core dumped) on...
4
by: pycraze | last post by:
Hello , i am working on NetBSD 3.0.2 with gcc version 3.3.3 . While debugging with gdb, i am getting a segmentation fault with accept system call and gdb is showing following traceback. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.