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 9 1289
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
"John" <jo*********@yahoo.com> wrote in message
news:c3**************************@posting.google.c om... 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;
}
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
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
Xiaobin Yang wrote: 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
"Jeff Flinn" <NO****@nowhere.com> wrote in message news:<ca**********@bluegill.adi.com>... "John" <jo*********@yahoo.com> wrote in message news:c3**************************@posting.google.c om... 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
"John" <jo*********@yahoo.com> wrote in message
news:c3**************************@posting.google.c om... "Jeff Flinn" <NO****@nowhere.com> wrote in message
news:<ca**********@bluegill.adi.com>... "John" <jo*********@yahoo.com> wrote in message news:c3**************************@posting.google.c om... 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
In message <c3**************************@posting.google.com >, John
<jo*********@yahoo.com> writes "Jeff Flinn" <NO****@nowhere.com> wrote in message news:<ca**********@bluegill.adi.com>... "John" <jo*********@yahoo.com> wrote in message news:c3**************************@posting.google.c om... > 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.
--
Richard Herring
"Jeff Flinn" <NO****@nowhere.com> wrote: "John" <jo*********@yahoo.com> wrote: "Jeff Flinn" <NO****@nowhere.com> wrote: "John" <jo*********@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:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting
Thanks a lot.
John
Richard Herring <ju**@[127.0.0.1]> wrote in message news:<MS**************@baesystems.com>... In message <c3**************************@posting.google.com >, John <jo*********@yahoo.com> writes"Jeff Flinn" <NO****@nowhere.com> wrote in message news:<ca**********@bluegill.adi.com>... "John" <jo*********@yahoo.com> wrote in message news:c3**************************@posting.google.c om... > 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: cppaddict |
last post by:
Hi,
I am deleting some objects created by new in my class destructor, and
it is causing my application to error at runtime. The code below
compiles ok, and also runs fine if I remove the body...
|
by: Johnny Hansen |
last post by:
Hello,
I've been trying to implement smart pointers in C++ (combined with a
reference counter) because I want to do some memory management. My code
is based on the gamedev enginuity articles,...
|
by: lothar.behrens |
last post by:
Hi,
I have the following definition of a pure abstract class:
class lb_I_String {
// ...
virtual lb_I_String* LB_STDCALL operator += (const char* toAppend) =
0;
|
by: Susan Baker |
last post by:
Hi,
I'm just reading about smart pointers..
I have some existing C code that I would like to provide wrapper classes
for. Specifically, I would like to provide wrappers for two stucts
defined...
|
by: Vijai Kalyan |
last post by:
I have been thinking about this and it may have already been thrashed
out and hung out to dry as a topic of no more interest but here goes.
I found when implementing a smart pointer that the...
|
by: Axter |
last post by:
I normally use a program call Doxygen to document my source
code.(http://www.stack.nl/~dimitri/doxygen)
This method works great for small and medium size projects, and you can
get good...
|
by: Yong Hu |
last post by:
Does the smart pointer never lead to memory leak?
Thanks,
|
by: Ney André de Mello Zunino |
last post by:
Hello.
I have written a simple reference-counting smart pointer class template
called RefCountPtr<T>. It works in conjunction with another class,
ReferenceCountable, which is responsible for the...
|
by: mati-006 |
last post by:
Hi
Why there is no "counted_ptr& operator= (pointee_type* p)" ?
This question has arisen when I was searching why following piece of
code won't compile:
arg::counted_ptr<testc;
c = new test;
...
|
by: Juha Nieminen |
last post by:
I asked a long time ago in this group how to make a smart pointer
which works with incomplete types. I got this answer (only relevant
parts included):
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |