This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: void pointer and map


Anitha Boyapati wrote:
> Below is a minimisation of a larger program (that may explain why the 
> objective of the progarm is not too clear). The essence is I am trying to 
> do a map find with a structure as key and the significant aspect of the 
> structure is - it has a void *. I expect the below progarm to print 
> nothing as s2 is not there on the map at all! There are 2 findings 
> however:

The one thing I can see is that you don't have a copy-constructor.
Add the following lines to Value and it doesn't segfault nor print
anything anymore (for me at least):

	Value (const Value &v) {
		id = v.id;
		buf = operator new(128);
		std::memcpy(buf, v.buf, 128);
	}

If you don't do this, g++ will generate a default copy
constructor, which merely copies over all fields.  This will make
several Value objects contain the same buf member pointing at the
same allocated memory, which uppon destruction will be deallocated
multiple times.

The use of that copy-constructor isn't obvious, but std::map
uses it.  (Try to declare it protected to make it obvious that
it's required.)

Hope this helps,
jlh


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]