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

F

Fei Liu

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::string, value_type> entries;
};

template <typename value_type>
std::string http_form<value_type>::get_form_data(){
typename std::map<std::string, value_type>::iterator 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_form<value_type>::http_form(){
}

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

#ifdef TEST_HTTP_FORM
using std::string;
int main(){
feiliu::http_form<string> form;
form.add_entry("hello", "world");
form.add_entry("foo", "bar");
form.get_form_data();
}
#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
 
A

Alf P. Steinbach

* 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.
 
F

Fei Liu

Alf said:
* Fei Liu:

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top