beginner question about auto_ptr

L

Lloyd Dupont

I try to use the auto_ptr in my code but that doesn't work.
even std::auto_ptr.
I guess I have to add an #include statement, but I can't figure out the
right file to add.

Also I want to use auto_ptr not to manage a single int but and int array.
something like
std::auto_ptr<int> adv = new int[42];

but from what I read auto_ptr doesn't seems to be the right solution for the
job.

basically I want to use auto_ptr so if I have exception in a constructore or
in a method, all memory is cleanly deallocated.
without me bothering writing heaps of try/catch/finally


any tip?
I'm working in Managed C++
 
?

=?ISO-8859-2?Q?Mihajlo_Cvetanovi=E6?=

Lloyd said:
I try to use the auto_ptr in my code but that doesn't work.
even std::auto_ptr.
I guess I have to add an #include statement, but I can't figure out the
right file to add.

The MSDN says it's said:
Also I want to use auto_ptr not to manage a single int but and int array.
something like
std::auto_ptr<int> adv = new int[42];

auto_ptr can't be used on plain arrays, because it calls delete, and
arrays need delete[]. You can instantiate std::vector instead of int[]:

std::auto_ptr<std::vector<int> > adv = new std::vector<int>(42);

(*adv)[2] = 2;
adv->operator[](3) = 3;
adv.get()->at(4) = 4;

If this vector has a function scope (it isn't used outside the function)
you can instantiate it on the stack:

I'm working in Managed C++

I'm not, but it's the same (I believe).
 
?

=?ISO-8859-2?Q?Mihajlo_Cvetanovi=E6?=

Mihajlo said:
std::auto_ptr<std::vector<int> > adv = new std::vector<int>(42);

Sorry, that's
std::auto_ptr<std::vector<int> > adv( new std::vector<int>(42) );
 
T

Tom Widmer [VC++ MVP]

Lloyd said:
I try to use the auto_ptr in my code but that doesn't work.
even std::auto_ptr.
I guess I have to add an #include statement, but I can't figure out the
right file to add.

#include said:
Also I want to use auto_ptr not to manage a single int but and int array.
something like
std::auto_ptr<int> adv = new int[42];

std::vector said:
but from what I read auto_ptr doesn't seems to be the right solution for the
job.

No, it only handles single elements.

Tom
 
G

Gabest

std::auto_ptr<std::vector<int> > adv = new std::vector<int>(42);

vector is a dynamic array already, what's the point allocating an additional
pointer to it? Unless you wanted to demostrate the usage of auto_ptr, I
can't see a reason doing it that way.
 
?

=?ISO-8859-2?Q?Mihajlo_Cvetanovi=E6?=

Gabest said:
vector is a dynamic array already, what's the point allocating an additional
pointer to it? Unless you wanted to demostrate the usage of auto_ptr, I
can't see a reason doing it that way.

The vector's assignment operator always copies elements from source to
destination, and vector by itself doesn't have detaching ability (to
reattach the inner array to another vector). If you have huge vectors
this copying (when you actually want "transfer of ownership") may hinder
your performance.

Apart from performance I can't think of any other reason.
 
T

Tom Widmer [VC++ MVP]

Mihajlo said:
The vector's assignment operator always copies elements from source to
destination, and vector by itself doesn't have detaching ability (to
reattach the inner array to another vector). If you have huge vectors
this copying (when you actually want "transfer of ownership") may hinder
your performance.

Actually, vector does have a detaching ability - v2.swap(v1);

Tom
 
?

=?ISO-8859-2?Q?Mihajlo_Cvetanovi=E6?=

Tom said:
Actually, vector does have a detaching ability - v2.swap(v1);

Oh, <blushing>, then I guess there's no reason to use it that way...

But what if I want to assign vector to a list box item data? Or if I
want to pass a vector to a new thread?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top