Main Page | Report Page

 

 

  Computers Forum Index » Computer Languages (C++) » Question about smart pointer

Author Message
John
Posted: Mon Jun 07, 2004 10:15 pm
 
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 Yang
Posted: Mon Jun 07, 2004 10:23 pm
 
can you give the declaration/implementation of "template <class T> class
auot_ptr {.....}"?

On Mon, 7 Jun 2004, John wrote:

Quote:
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
 
Jeff Flinn
Posted: Mon Jun 07, 2004 10:28 pm
 
"John" <johnw822003@yahoo.com> wrote in message
news:c30e885a.0406071015.6f9aa158@posting.google.com...
Quote:
Hi all:

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

[see below]

Quote:
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()

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

return p;

> }
 
Alan Johnson
Posted: Mon Jun 07, 2004 10:45 pm
 
John wrote:
Quote:
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
 
Alan Johnson
Posted: Mon Jun 07, 2004 10:45 pm
 
Xiaobin Yang wrote:

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

On Mon, 7 Jun 2004, John wrote:


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

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

Alan
 
John
Posted: Tue Jun 08, 2004 2:03 am
 
"Jeff Flinn" <NONONE@nowhere.com> wrote in message news:<ca2c4m$909$1@bluegill.adi.com>...
Quote:
"John" <johnw822003@yahoo.com> wrote in message
news:c30e885a.0406071015.6f9aa158@posting.google.com...
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?

Thanks.

JOhn
 
Jeff Flinn
Posted: Tue Jun 08, 2004 2:15 am
 
"John" <johnw822003@yahoo.com> wrote in message
news:c30e885a.0406071403.311202df@posting.google.com...
Quote:
"Jeff Flinn" <NONONE@nowhere.com> wrote in message
news:<ca2c4m$909$1@bluegill.adi.com>...
"John" <johnw822003@yahoo.com> wrote in message
news:c30e885a.0406071015.6f9aa158@posting.google.com...
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?


Wrong.

Jeff F
 
Dietmar Kuehl
Posted: Tue Jun 08, 2004 5:13 pm
 
"Jeff Flinn" <NONONE@nowhere.com> wrote:
Quote:
"John" <johnw822003@yahoo.com> wrote:
"Jeff Flinn" <NONONE@nowhere.com> wrote:
"John" <johnw822003@yahoo.com> wrot:
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?

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'.
--
<mailto:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting
 
 
Page 1 of 1    
All times are GMT
The time now is Wed May 16, 2012 10:17 pm