Re: auto_ptr by Ulrich
Ulrich
Thu Nov 24 08:08:31 CST 2005
Mark wrote:
> struct A
> {
> int a;
> int b;
> int c;
> };
>
> Now I define an auto_ptr to point to a new area of memory...
>
> auto_ptr<A> a = new char[20];
>
> Now when I do...
>
> delete a;
>
> Does it delete 12 bytes (the size of A) or 20 bytes (the size allocated)?
>
This doesn't even compile, I daresay. Anyhow, the point of std::auto_ptr<>
is that you don't have to call delete yourself. Once initialized, the
pointee belongs to the auto_ptr and will be deleted by it. Please also read
up on the semantics of copying std::auto_ptrs, they are surprising at
first.
If you want to use it for automated buffer management, I'm afraid it won't
work. For that, take a look at the smart pointer library of Boost (or TR1),
in particular shared_array and scoped_array.
Uli