That would be a violation of a class invariant. There should probably be an assert there to make sure the invariant holds, but no special handling code is required.
That would be a violation of a class invariant. There should probably be an assert there to make sure the invariant holds, but no special handling code is required.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
That's called User Error.
If you lie to a function, expect bad things.
An assert would do the trick. But the objective is to work with the library client, not against him.
Undefined behavior is pretty harsh punishment.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
assert() only works in debug mode, so unless they actually test with the debug build it won't help; but I'll add it anyways.
But then how do you stop the user from entering and oldSize that's bigger than it really is, or passing an invalid (not NULL) pointer?
Now if anyone has any ideas on how to do this on UNIX, that would be really cool!
I don't really understand this, though:
Doesn't that basically mean construct a new object instead of copying it? Or maybe that basically invokes the copy constructor, I think?Code:new( &pNew[i] ) T( ptr[i] );
I'm also rusty on that syntax... but new returns a pointer to an allocated object... but I'm guessing that it is just supposed to create an object in the allocated memory and invoke the (copy) constructor?
Just a thought: but your realloc function requires memory to be allocated with HeapAlloc to work. And if such is the case, you can use HeapSize to get size (no OldSize) and to see if it's a valid pointer.
Well, what do you think copying is? It's creating a new object equivalent to an existing one.Doesn't that basically mean construct a new object instead of copying it?
new returns a pointer to the allocated object. But in the case of placement new, this pointer is equal to the one we passed to it, so keeping it is unnecessary.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
I was confused about if it was copying the state of the object over, as well, since new would just invoke the constructor.
So this is placement new then. I see.new returns a pointer to the allocated object. But in the case of placement new, this pointer is equal to the one we passed to it, so keeping it is unnecessary.
It would invoke the constructor. The copy constructor.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law