473,786 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

a bug in g++ std::map implementation?

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 client problem)
#ifndef HTTP_FORM_H
#define HTTP_FORM_H

#include <map>
#include <string>
#include <iostream>

namespace feiliu{
template <typename value_type>
class http_form{
public:
http_form();
~http_form();
void add_entry(const std::pair<std:: string, value_type&
entry){
entries.insert( entry);
}
void add_entry(const std::string & key, const value_type &
value){
entries.insert( std::pair<std:: string, value_type>(key ,
value));
}
std::string get_form_data() ;
private:
std::map<std::s tring, value_typeentri es;
};

template <typename value_type>
std::string http_form<value _type>::get_for m_data(){
typename std::map<std::s tring, value_type>::it erator iter =
entries.begin() ;
std::cout << "size of map: " << entries.size() << '\n';
for(int i = 0; i < entries.size(); i ++){
std::cout << "key = " << iter->first << " value = " <<
iter->second << '\n';
++ iter;
}
for(iter = entries.begin() ; iter != entries.end(); ++ iter)
std::cout << "key = " << iter->first << " value = " <<
iter->second << '\n';
}
}

#endif

http_form.cpp:
#include "http_form. h"
template <typename value_type>
feiliu::http_fo rm<value_type>: :http_form(){
}

//std::string http_form::get_ form_data(){
template <typename value_type>
feiliu::http_fo rm<value_type>: :~http_form(){
entries.clear() ;
}

#ifdef TEST_HTTP_FORM
using std::string;
int main(){
feiliu::http_fo rm<stringform;
form.add_entry( "hello", "world");
form.add_entry( "foo", "bar");
form.get_form_d ata();
}
#endif

compile: g++ -DTEST_HTTP_FORM -g -O0 -o tf http_form.cpp
run: ./tf
size of map: 2
key = foo value = bar
key = hello value = world
key = foo value = bar
key = hello value = world
Segmentation fault
Apparently, there is something wrong with iterator using begin() and
end()...Any help is appreciated. Thanks,

Fei
Jan 18 '07 #1
2 2845
* Fei Liu:
>
compile: g++ -DTEST_HTTP_FORM -g -O0 -o tf http_form.cpp
Add options to up the warning level. Presumably the compiler will then
complain about a missing return statement.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jan 18 '07 #2

Alf P. Steinbach wrote:
* Fei Liu:

compile: g++ -DTEST_HTTP_FORM -g -O0 -o tf http_form.cpp

Add options to up the warning level. Presumably the compiler will then
complain about a missing return statement.
Ah, that did the trick, adding return "" stopped the compiler from
generating bad assembly. Thanks,

Fei

Jan 19 '07 #3

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

Similar topics

1
1872
by: Antti Granqvist | last post by:
Hello! I have following object relations: Competition 1--* Category 1--* Course 1 | | * Course
44
8810
by: jmoy | last post by:
I am a C programmer graduating to C++. As an exercise I wrote a program to count the number of times that different words occur in a text file. Though a hash table might have been a better choice, I chose to use std::map. However, the program runs at less than half the speed of an equivalent program that uses ordinary handcoded binary trees. The result is not changed very much if I replace std::string as the key with a simple string class...
5
426
by: EnTn | last post by:
Hi Everyone... I've been trying to use a std::map to do some storage. Basically, i'm storing double values using a Key Object. The Key object is quite simple and is just a pair of int's (conceptually these are 2d coords). My problem is: I have setup a simple test probgram that populates a map with 9 <key,value> pairs. When I try to query the map using
1
3554
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 program uses two MFC classes: CRect and CPoint which represents Rectangle and Point concepts (as usual) and a user
5
8753
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) throw(std::runtime_error)
13
4643
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 which also implements ==, <, <=, >, >=, etc. (Those operators are tested and working correctly.) If I assign map = "world", it saves the MyString's correctly in the map. But a subsequent call to map.find("hello") returns map.end(). Even more...
3
3716
by: Dan Trowbridge | last post by:
Hi everyone, In my attempt to port code from VS 6.0 to VS.NET I had some code break along the way, mostly due to not adhereing closely to the C++ standard. This may be another instance but I can't think of a good fix, or even why it broke. The problem In one of my CFormView derived classes I have a member variable of the type...
13
9678
by: kamaraj80 | last post by:
Hi I am using the std:: map as following. typedef struct _SeatRowCols { long nSeatRow; unsigned char ucSeatLetter; }SeatRowCols; typedef struct _NetData
2
5146
by: nerdrakesh | last post by:
Hi - I am using the map stl and is there a way I could specify the hash table size in the map in the beginning - (similar to vector.reserve(size) ). Is there a way I could specify the entries at the beginning. Is there an implementation-independent hint that I could give to map so that it performs better when I add more entries to it.
0
9492
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10360
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10163
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10108
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.