core-dump with list copy

Discussion in 'C++' started by sam, May 15, 2005.

  1. sam

    sam Guest

    Hi,

    The following code produced a core-dump:
    PropertyParser::propertyParser(list<HashMap> &p_list)
    {
    l_conf_data.clear();
    cout << "beginning copy.. size: " << p_list.size() << endl;
    copy(p_list.begin(), p_list.end(), l_conf_data.begin());
    cout << "finished copy..." << endl;
    }

    The above code print out "beginning copy... size: 21", then crash with
    core-dump.

    What can I do to track down where the problem is?

    Thanks
    Sam
     
    sam, May 15, 2005
    #1
    1. Advertising

  2. sam

    Kai-Uwe Bux Guest

    sam wrote:

    > Hi,
    >
    > The following code produced a core-dump:
    > PropertyParser::propertyParser(list<HashMap> &p_list)
    > {
    > l_conf_data.clear();
    > cout << "beginning copy.. size: " << p_list.size() << endl;
    > copy(p_list.begin(), p_list.end(), l_conf_data.begin());
    > cout << "finished copy..." << endl;
    > }
    >
    > The above code print out "beginning copy... size: 21", then crash with
    > core-dump.
    >
    > What can I do to track down where the problem is?


    First, you can post more code: try to trim your code to a minimal piece
    that still reproduces the error, but that we can compile, run, and analyze.
    Then someone here might be able to help. The code above contains too many
    unknowns, in particular the copy constructor and assignment operator of
    "HashMap" that will be called by copy are missing in the picture. Also, we
    cannot even see the type of "_l_conf_data". Please, post more code.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, May 15, 2005
    #2
    1. Advertising

  3. sam

    Guest

    15.05.2005 18:53 answer to:
    sam <> wrote:
    > Hi,
    >
    > The following code produced a core-dump:
    > PropertyParser::propertyParser(list<HashMap> &p_list)
    > {
    > l_conf_data.clear();
    > cout << "beginning copy.. size: " << p_list.size() << endl;
    > copy(p_list.begin(), p_list.end(), l_conf_data.begin());
    > cout << "finished copy..." << endl;
    > }
    >
    > The above code print out "beginning copy... size: 21", then crash with
    > core-dump.
    >
    > What can I do to track down where the problem is?
    >
    > Thanks
    > Sam


    From the code snippet above I cannot see the details of l_conf_data and its contents, but I'd suggest you re-think if that copy
    statement really does what you intend to do. What happens if l_conf_data cannot contain all the data you would like to
    copy into it? You somehow have to take care of this situation by either allocating enough memory beforehand or use a
    back_inserter.

    HTH
    Chris
     
    , May 15, 2005
    #3
  4. sam

    sam Guest

    OK, I try to make it simple:

    class HashMap: public hash_map<string, string, HashString,
    HashStringCompare>
    {
    public:
    HashMap(): hash_map<string, string, HashString, HashStringCompare>() {}
    };

    class PropertyParser
    {
    public:
    PropertyParser(list<HashMap> &p_list);

    private:
    list<HashMap> l_conf_data;
    };

    PropertyParser::propertyParser(list<HashMap> &p_list)
    {
    l_conf_data.clear();
    copy(p_list.begin(), p_list.end(),
    l_conf_data.begin()
    );
    }

    I read somewhere about some example as shown below, but couldn't get
    more detail about it.
    copy (coll1.begin(), coll1.end(), // source range
    back_inserter(coll2)); // destination range

    Thanks
    Sam


    Kai-Uwe Bux wrote:

    > sam wrote:
    >
    >
    >>Hi,
    >>
    >>The following code produced a core-dump:
    >>PropertyParser::propertyParser(list<HashMap> &p_list)
    >>{
    >> l_conf_data.clear();
    >>cout << "beginning copy.. size: " << p_list.size() << endl;
    >> copy(p_list.begin(), p_list.end(), l_conf_data.begin());
    >>cout << "finished copy..." << endl;
    >>}
    >>
    >>The above code print out "beginning copy... size: 21", then crash with
    >>core-dump.
    >>
    >>What can I do to track down where the problem is?

    >
    >
    > First, you can post more code: try to trim your code to a minimal piece
    > that still reproduces the error, but that we can compile, run, and analyze.
    > Then someone here might be able to help. The code above contains too many
    > unknowns, in particular the copy constructor and assignment operator of
    > "HashMap" that will be called by copy are missing in the picture. Also, we
    > cannot even see the type of "_l_conf_data". Please, post more code.
    >
    >
    > Best
    >
    > Kai-Uwe Bux
     
    sam, May 15, 2005
    #4
  5. sam

    Guest

    15.05.2005 19:16 answer to:
    sam <> wrote:
    > OK, I try to make it simple:
    >
    > class HashMap: public hash_map<string, string, HashString,
    > HashStringCompare>
    > {
    > public:
    > HashMap(): hash_map<string, string, HashString, HashStringCompare>() {}
    > };
    >
    > class PropertyParser
    > {
    > public:
    > PropertyParser(list<HashMap> &p_list);
    >
    > private:
    > list<HashMap> l_conf_data;
    > };
    >
    > PropertyParser::propertyParser(list<HashMap> &p_list)
    > {
    > l_conf_data.clear();
    > copy(p_list.begin(), p_list.end(),
    > l_conf_data.begin()
    > );
    > }
    >
    > I read somewhere about some example as shown below, but couldn't get
    > more detail about it.
    > copy (coll1.begin(), coll1.end(), // source range
    > back_inserter(coll2)); // destination range


    Please don't top-post. Yes, the code above is the way to go. See my other post for an explanation.

    Cheers
    Chris
    >
     
    , May 15, 2005
    #5
  6. sam

    Mike Wahler Guest

    "sam" <> wrote in message news:d67vdg$ede$...
    > Hi,
    >
    > The following code produced a core-dump:
    > PropertyParser::propertyParser(list<HashMap> &p_list)
    > {
    > l_conf_data.clear();
    > cout << "beginning copy.. size: " << p_list.size() << endl;
    > copy(p_list.begin(), p_list.end(), l_conf_data.begin());
    > cout << "finished copy..." << endl;
    > }
    >
    > The above code print out "beginning copy... size: 21", then crash with
    > core-dump.
    >
    > What can I do to track down where the problem is?


    'std::copy' can only copy elements to already existing
    container elements; IOW it 'overwrites' existing elements.
    Above you're trying to overwrite elements which do not
    exist. This produces undefined behavior.

    Look up 'std::insert_iterator', or 'std::inserter'.

    'std::insert_iterator' is a special iterator type which
    when used with 'std::copy', create a new element before
    copying to it. 'std::inserter' is a function which returns
    a 'std::insert_iterator'. See a C++ text and/or your standard
    library documentation for more details.

    -Mike
     
    Mike Wahler, May 15, 2005
    #6
    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. Mike
    Replies:
    0
    Views:
    711
  2. halfdog
    Replies:
    12
    Views:
    12,454
  3. Jaydeep Chovatia

    core dump in std::list push_back

    Jaydeep Chovatia, Mar 24, 2011, in forum: C++
    Replies:
    9
    Views:
    1,205
    Angus
    Mar 25, 2011
  4. Pallav singh
    Replies:
    1
    Views:
    266
    Victor Bazarov
    Aug 21, 2011
  5. Pallav singh
    Replies:
    0
    Views:
    263
    Pallav singh
    Aug 20, 2011
Loading...

Share This Page