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)?

--
Best regards
Mark

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


Re: auto_ptr by Tom

Tom
Thu Nov 24 08:14:50 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];

The line above doesn't compile. You need:

auto_ptr<A> a(new A); //or new A()

> Now when I do...
>
> delete a;
>
> Does it delete 12 bytes (the size of A) or 20 bytes (the size allocated)?

The whole point of auto_ptr is that you don't need to call delete on it
- it deletes the memory automatically when it goes out of scope. Also,
note that you can't use new[] with auto_ptr, only single object new,
since it uses delete to free the memory, not delete[].

Tom

Re: auto_ptr by Igor

Igor
Thu Nov 24 08:15:33 CST 2005

"Mark" <swozz_@hotmail.com> wrote in message
news:%23napw8P8FHA.2040@TK2MSFTNGP14.phx.gbl
> 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];

That should not compile, nor does it make any sense. What are you trying
to do?

> Now when I do...
>
> delete a;

The whole point of auto_ptr is so that you don't need to explicitly
delete.

> Does it delete 12 bytes (the size of A) or 20 bytes (the size
> allocated)?

Seeing how your code does not compile, the question is moot.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925