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

Iterating on std::map results in segmentation fault

Expand|Select|Wrap|Line Numbers
  1. void call(function<void ()> f) {
  2.     f();
  3. }
  4. void end() {
  5.     cout << "End..." << endl;
  6. }
  7. void fn(map<string, string> &m, map<string, string>::iterator i) {
  8.     cout << "i->first: " << i->first << endl;
  9.     if (++i == m.end()) {
  10.         end();
  11.     } else {
  12.         call(bind(&fn, m, i));
  13.     }
  14. }
  15. int main() {
  16.     map<string, string> m;
  17.     m["abc"] = "asasd";
  18.     m["bcd"] = "sasas";
  19.     m["cde"] = "jdssd";
  20.  
  21.     call(bind(&fn, m, m.begin()));
  22. }
  23.  
The above code results in segmentation fault. Kindly help me find where im I wrong.
Apr 26 '10 #1
2 5715
in fn function you are trying to access the first element of the iterator without checking its value (when it reaches the end of map, it will cause segmentation if you try to print), try this

Expand|Select|Wrap|Line Numbers
  1. if (i != m.end())
  2.      cout << "i->first: " << i->first << endl; 
  3. }
Apr 26 '10 #2
@akdemirc
Thanks for your kind reply. Sorry for not being clear with my question. Im not considering the case when map is empty it ll be handled separately.
The following code will excutes fine.
Expand|Select|Wrap|Line Numbers
  1. void fn(map<string, string> &m, map<string, string>::iterator i) {
  2.     cout << "i->first: " << i->first << endl;
  3.     if (++i == m.end()) {
  4.         end();
  5.     } else {
  6.         // call(bind(&fn, m, i));
  7.         fn(m, i);
  8.     }
  9. }
  10. int main() {
  11.     map<string, string> m;
  12.     m["abc"] = "asasd";
  13.     m["bcd"] = "sasas";
  14.     m["cde"] = "jdssd";
  15.  
  16.     // call(bind(&fn, m, m.begin()));
  17.     fn(m, m.begin());
  18. }
  19.  
But with callbacks why am I getting a segmentation fault?
Apr 26 '10 #3

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

Similar topics

8
by: Kin Pang | last post by:
Hi, I have a routine where I'm using a std::map from a pair of ints to double for caching evaluation results. The cache depth could be upto say 1000 in depth. However, my application needs to...
25
by: Christopher Benson-Manica | last post by:
If you liked the functionality of std::map, but found that you couldn't trust your implementation to handle nonstandard data structures, how would you code a wrapper? I've produced the following:...
2
by: Serengeti | last post by:
Hello, in my class I have a map that translates strings to pointers to some member functions. The code goes like this: class F { typedef void (Function::*MathFuncPtr)(); std::map<std::string,...
1
by: Saeed Amrollahi | last post by:
Dear All C++ Programmers Hello I am Saeed Amrollahi. I am a software engineer in Tehran Sewerage Company. I try to use std::map and map::find member function. I use Visual Studio .NET. my...
5
by: Peter Jansson | last post by:
Hello, I have the following code: std::map<int,std::set<std::string> > k; k="1234567890"; k="2345678901"; //... std::set<std::string> myMethod(std::map<int,std::set<std::string> > k)...
13
by: jstanforth | last post by:
This is probably a very obvious question, but I'm not clear on what operators need to be implemented for std::map.find() to work. For example, I have a class MyString that wraps std::string, and...
19
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not...
3
by: Jonathan Lamothe | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hey all. I've got a problem I can't seem to figure out. I'm writing a program that deals with money in multiple currencies. In order to define...
2
by: Fei Liu | last post by:
Hello, I am getting something really wierd out of this source code, can you please verify if you also get segmentation fault (memory failure)? http_form.h: (sorry about the line wrapping, email...
7
by: DevNull | last post by:
Hi there everyone, I'm creating a very simple immediate mode command interpreter. The final purpose is to provide a pluggable control and command console for a MUD server I have written. The...
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
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...
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
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.