Re: Wrong
On 04/18/2010 07:21 AM, Juha Nieminen wrote:
cpp4ever <n2xssvv.g02gfr12930@ntlworld.com> wrote:
Anyone doing that in production code is likely to create problems.
Can you mention even one implementation where, when using basic types
and without the compiler explicitly adding debug checks, that example code
would cause any kind of misbehavior?
Not currently, but do you think this sort of practice could not create
problems when the code is modified as required? Don't assume people are
perfect software developers and will spot this potential trap.
More
to the point,this is for the simple case of an int, consider the use of
this technique with a class object
You can't do it for objects because that memory is uninitialized. You
can't use objects which haven't been constructed in the first place.
Ever heard of overloading the operator = (), or use of appropriate
constructor?
I think you mean you shouldn't, I've seen code where someone used part
of some malloc allocated memory as a std::vector which inevitably caused
memory leakage as it was never destroyed properly.
, the new object created would not be
destroyed which could lead to memory leaks.
The problem starts earlier than that, since the object has not been
constructed in the first place.
Of course you could use placement new and then call a destructor
explicitly, but I don't understand why you would want to.
I do hope you're not defending this technique, as IMHO it is a terrible
thing to do, using any types.
JB