Hello,
I am getting very strange behavior on an STL set::insert() .

...
		//Rebuild the P_set
		P_set.clear();//destroy old P set (shallow only)
		assert( P_set.empty() );
		for(int i = 0 ; i < N ; i++){
			cout<<"addr="<< &(C_array[i])<<endl;
			pair<set<C*>::iterator,bool> p = P_set.insert( &(C_array[i]) );
			assert( p.second && "FAILED TO INSERT" );
		} 	
		assert( P_set.size() == N );
...

Very occasionally I fail the assert( p.second && "FAILED TO INSERT" );

No idea why, I print the addr out and they are all unique.

Any ideas?

Post all your code. Maybe I can help. Did you found any error?

Post all your code. Maybe I can help. Did you found any error?

I can't post my code.

But this little section clears the set, and then iterates thru an array, using each addr as a ptr. How could that fail to insert? It would mean the addr was not unique, but how is that possible here?

Remember a set can only have distinct elements, that might be the reason why it fails sometimes.

Remember a set can only have distinct elements, that might be the reason why it fails sometimes.

But the set is of C*, and they have to be unique because it iterates thru and array and gets addresses

Do you check if p.first points to the correct element?

Do you check if p.first points to the correct element?

Wait, is it possible that it is somehow looking at the value? Here is some output right before it fails:

...
0: addr=0xe62a58 val=4.05578e-05
0: addr=0xe62a98 val=5.2957e-05
0: addr=0xe62ad8 val=4.95495e-05
0: addr=0xe62b18 val=5.2957e-05
...

Could the compare function, somehow be causing it to look at the value?

Do you specify your own compare function or use the default?

Do you specify your own compare function or use the default?

I specify my own.

I made it a multiset and it seems to work on a few testcases....crossing my fingers hoping it will work on all.

How about posting your compare function? That seems to me to be the most likely cause of the problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.