core-dump with list copy

S

sam

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
 
K

Kai-Uwe Bux

sam said:
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
 
C

Chris.Theis

15.05.2005 18:53 answer to:
sam said:
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
 
S

sam

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
 
C

Chris.Theis

15.05.2005 19:16 answer to:
sam said:
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
 
M

Mike Wahler

sam said:
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
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top