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

Discussion in 'C++' started by Fei Liu, Jan 18, 2007.

  1. Fei Liu

    Fei Liu Guest

    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
     
    Fei Liu, Jan 18, 2007
    #1
    1. Advertising

  2. * 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?
     
    Alf P. Steinbach, Jan 18, 2007
    #2
    1. Advertising

  3. Fei Liu

    Fei Liu Guest

    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
     
    Fei Liu, Jan 19, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthias Hildebrand
    Replies:
    5
    Views:
    8,012
    krogers
    Mar 20, 2012
  2. Peter Jansson
    Replies:
    5
    Views:
    6,355
    Ivan Vecerina
    Mar 17, 2005
  3. Replies:
    1
    Views:
    435
    red floyd
    Dec 21, 2008
  4. Thomas J. Gritzan
    Replies:
    6
    Views:
    1,033
    James Kanze
    Dec 22, 2008
  5. James Kanze
    Replies:
    0
    Views:
    2,028
    James Kanze
    Dec 21, 2008
Loading...

Share This Page