Boost logo

Boost :

Subject: Re: [boost] How to create a shallow copy without callingaconstructor?
From: Peter Dimov (pdimov_at_[hidden])
Date: 2010-01-04 13:08:08


Stefan Strasser wrote:

> quick question about this, is it legal to do the following?
>
> T *t=reinterpret_cast<T *>(new char[sizeof(T)]);
> new (&t) T;
> delete T;

No, because memory allocated with new[] must be deallocated with delete[].

T *t=static_cast<T *>( operator new( sizeof(T) ) );

takes care of this. There'd still be a mismatch if T had class-specific
operator new/delete though, unless one is careful to use ::delete.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk