Question about smart pointer

J

John

Hi all:

Just learn to use smart pointer. I get a question. In the following
code:
void foo()
{
auto_ptr<MyClass> p(new MyClass);
p->DoSomething();
}

By the end of function foo(), the memory that is allocated to p will
be automatically deallocated. But if I have other functions that need
to access the memory allocated to p, and eventually that memory will
be deallocated by one function. So I do not want the memory allocated
to p to be deallocated by the end of function foo(). What should I do?

Thanks a lot.

John
 
X

Xiaobin Yang

can you give the declaration/implementation of "template <class T> class
auot_ptr {.....}"?

Hi all:

Just learn to use smart pointer. I get a question. In the following
code:
void foo()
{
auto_ptr<MyClass> p(new MyClass);
p->DoSomething();
}

By the end of function foo(), the memory that is allocated to p will
be automatically deallocated. But if I have other functions that need
to access the memory allocated to p, and eventually that memory will
be deallocated by one function. So I do not want the memory allocated
to p to be deallocated by the end of function foo(). What should I do?

Thanks a lot.

John

-- xiaobin
 
J

Jeff Flinn

John said:
Hi all:

Just learn to use smart pointer. I get a question. In the following
code:

[see below]
By the end of function foo(), the memory that is allocated to p will
be automatically deallocated. But if I have other functions that need
to access the memory allocated to p, and eventually that memory will
be deallocated by one function. So I do not want the memory allocated
to p to be deallocated by the end of function foo(). What should I do?
void foo()

auto_ptr said:
{
auto_ptr<MyClass> p(new MyClass);
p->DoSomething();

return p;
 
A

Alan Johnson

John said:
Hi all:

Just learn to use smart pointer. I get a question. In the following
code:
void foo()
{
auto_ptr<MyClass> p(new MyClass);
p->DoSomething();
}

By the end of function foo(), the memory that is allocated to p will
be automatically deallocated. But if I have other functions that need
to access the memory allocated to p, and eventually that memory will
be deallocated by one function. So I do not want the memory allocated
to p to be deallocated by the end of function foo(). What should I do?

Thanks a lot.

John

The release() method will cause the auto_ptr to give up ownership of the
memory. For example:

void foo()
{
auto_ptr<MyClass> p(new MyClass);
MyClass *t;

p->DoSomething();

// Other stuff.

t = p.release();

// Now we must manually delete the memory.
delete t;
}

Alan
 
A

Alan Johnson

Xiaobin said:
can you give the declaration/implementation of "template <class T> class
auot_ptr {.....}"?




-- xiaobin

std::auto_ptr<T> is in <memory>.

Alan
 
J

John

Jeff Flinn said:
John said:
Hi all:

Just learn to use smart pointer. I get a question. In the following
code:

[see below]
By the end of function foo(), the memory that is allocated to p will
be automatically deallocated. But if I have other functions that need
to access the memory allocated to p, and eventually that memory will
be deallocated by one function. So I do not want the memory allocated
to p to be deallocated by the end of function foo(). What should I do?
void foo()

auto_ptr said:
{
auto_ptr<MyClass> p(new MyClass);
p->DoSomething();

return p;
}

But that allocated memory will still be deallocated by the end of foo(), right?

Thanks.

JOhn
 
J

Jeff Flinn

John said:
"Jeff Flinn" <[email protected]> wrote in message
John said:
Hi all:

Just learn to use smart pointer. I get a question. In the following
code:

[see below]
By the end of function foo(), the memory that is allocated to p will
be automatically deallocated. But if I have other functions that need
to access the memory allocated to p, and eventually that memory will
be deallocated by one function. So I do not want the memory allocated
to p to be deallocated by the end of function foo(). What should I do?
void foo()

auto_ptr said:
{
auto_ptr<MyClass> p(new MyClass);
p->DoSomething();

return p;
}

But that allocated memory will still be deallocated by the end of foo(),
right?

Wrong.

Jeff F
 
R

Richard Herring

John said:
Jeff Flinn said:
John said:
Hi all:

Just learn to use smart pointer. I get a question. In the following
code:

[see below]
By the end of function foo(), the memory that is allocated to p will
be automatically deallocated. But if I have other functions that need
to access the memory allocated to p, and eventually that memory will
be deallocated by one function. So I do not want the memory allocated
to p to be deallocated by the end of function foo(). What should I do?
void foo()

auto_ptr said:
{
auto_ptr<MyClass> p(new MyClass);
p->DoSomething();

return p;
}

But that allocated memory will still be deallocated by the end of foo(), right?
No. Ownership is transferred to the copy of the pointer returned by
foo(). The responsibility for deleting it now rests with the entity to
which it was returned.

More generally, you need to ask yourself about your ownership model. Is
the instance of MyClass solely owned by a single instance of some other
class (not necessarily the same one, but exactly one at any point in
time) or is it shared between several other objects, so it should be
kept in existence until all of them have finished with it?

std::auto_ptr is designed to work with the first case, but if what you
really want is the second, you should be looking at boost::shared_ptr.
 
D

Dietmar Kuehl

Jeff Flinn said:
right?

Wrong.

To elaborate a little bit: 'std::auto_ptr' transfers ownership of the
pointer when it is assigned or copied. That is, the pointer owned by 'p'
is returned to 'std::auto_ptr' returned from 'foo' and will be deleted
when this auto pointer is destructed - unless, of course, it is
transfered to yet another pointer.

The utility of 'std::auto_ptr' is relatively limited. In situations where
the ownership of a pointer is less clear, it is probably advisable to use
a reference counted pointer like eg. 'boost::shared_ptr' which is coming
up in the library TR as 'std::tr1::shared_ptr'.
 
J

John

Thanks a lot.

John

Richard Herring said:
John said:
Jeff Flinn said:
Hi all:

Just learn to use smart pointer. I get a question. In the following
code:

[see below]

By the end of function foo(), the memory that is allocated to p will
be automatically deallocated. But if I have other functions that need
to access the memory allocated to p, and eventually that memory will
be deallocated by one function. So I do not want the memory allocated
to p to be deallocated by the end of function foo(). What should I do?
void foo()

auto_ptr<MyClass> foo()

{
auto_ptr<MyClass> p(new MyClass);
p->DoSomething();

return p;
}

But that allocated memory will still be deallocated by the end of foo(), right?
No. Ownership is transferred to the copy of the pointer returned by
foo(). The responsibility for deleting it now rests with the entity to
which it was returned.

More generally, you need to ask yourself about your ownership model. Is
the instance of MyClass solely owned by a single instance of some other
class (not necessarily the same one, but exactly one at any point in
time) or is it shared between several other objects, so it should be
kept in existence until all of them have finished with it?

std::auto_ptr is designed to work with the first case, but if what you
really want is the second, you should be looking at boost::shared_ptr.
 

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

Members online

Forum statistics

Threads
474,170
Messages
2,570,924
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top