set_difference with lists strangeness

Hi, I have a bug when using set_difference with lists which puzzles me. The code runs like this: list<int> nids,gnids,rnids; //fill nids and gnids nids.sort(); gnids.sort(); set_difference(nids.begin(), nids.end(), gnids.begin(), gnids.end(), rnids.begin()); It works as expected in debug mode (-O0) but crashes in optimized mode (-O3). rnids is still empty after set_difference and the program crashes as soon as I try to use rnids. If I use a vector of the right size for rnids everything works. Obviously the memory allocation for the receiving list is optimized away. Is this not against the idea of the STL algorithms? Cheers, Torsten

AMDG Torsten Sadowski wrote:
Hi,
I have a bug when using set_difference with lists which puzzles me. The code runs like this:
list<int> nids,gnids,rnids;
//fill nids and gnids
nids.sort(); gnids.sort(); set_difference(nids.begin(), nids.end(), gnids.begin(), gnids.end(), rnids.begin());
It works as expected in debug mode (-O0) but crashes in optimized mode (-O3). rnids is still empty after set_difference and the program crashes as soon as I try to use rnids. If I use a vector of the right size for rnids everything works. Obviously the memory allocation for the receiving list is optimized away. Is this not against the idea of the STL algorithms?
This is not really a Boost question. try using std::back_inserter(rnids) for the output. In Christ, Steven Watanabe
participants (2)
-
Steven Watanabe
-
Torsten Sadowski