Re: Using auto_ptr with array of doubles in VS6 and VS2005
"Doug Harrison [MVP]" <dsh@mvps.org> wrote in message
news:u44b35ln102umu41s8goe7gig45v8eg04p@4ax.com...
On Sun, 14 Jun 2009 15:31:05 -0700 (PDT), Robert <rsa2112@gmail.com>
wrote:
I know it is improper to do something like this:
std::auto_ptr<double> var(new double[10]);
since it is undefined behavior to call delete on the above array
(created with new []) as the auto_ptr object will do when going out of
scope.
I have code like this that seems to work in both VS6 and VS2005. My
question is, what is Microsoft doing behind the scene to make auto_ptr
work for arrays of basic types in general? Thanks.
It works by coincidence. It's not guaranteed to work, and you should use
std::vector instead.
And as for why it looks like it works:
In current versions of MS VC++ compilers (no guarantees what the next patch
will do), calling scalar delete instead of vector delete results in the
memory being freed, but the destructors are not run for any element except
the first.
Naturally basic types have no destructor (or have a default destructor that
does nothing at all), so not running it is not particularly a problem.
Nevertheless, although VC++ does something reasonable with that broken code
today, the code is wrong and needs to be fixed.
__________ Information from ESET NOD32 Antivirus, version of virus signature database 4160 (20090616) __________
The message was checked by ESET NOD32 Antivirus.
http://www.eset.com