Re: what is the purpose of C++ smart pointer
- From: Carlos Moreno <moreno_at_mochima_dot_com@xxxxxxxxxxxxxx>
- Date: Wed, 25 Oct 2006 16:36:37 -0400
Wolfgang Draxinger wrote:
I know C++ has a concept that named smart pointer, its main
usage is to free the reference memory when the ref count
reaches 0(or weak, not so).
You confuse reference counting with smart pointers. A smart
pointer tracks the data it is referring to and updates itself
following the changes of the memory it points to. E.g. if you
move the data to another position in memory, a smart pointer
would follow it.
Unless I'm not understanding correctly, the above is deeply
wrong, and at so many levels!!
How exactly will the smart pointer know that you moved the
data?? Is there any "physical" association so that the data
has the value of the pointer attached to it??
True that the OP *is* indeed mixing the notions of smart pointers
and reference counting -- but the way I understand what you wrote,
you are confusing the notions of smart poinetrs with ... with ...
I don't even know what one could call that notion that you
describe.
To the OP:
Smart pointers is a generic name given to wrapper classes that
somewhat behave as a pointer (same semantics, common behaviour),
but with additional functionality. This additional functionality
is usually intended to relieve the programmer from the housekeeping
tasks related to memory management when pointers are involved
(memory leaks, memory corruption, double-deletion of allocated
memory, incorrect ownership management, etc.)
The simplest example of a smart pointer is the standard auto_ptr
class. Objects of this class behave exactly like a regular pointer
in terms of assignment, comparison, dereferencing -- but the class
provides (among other useful things) a destructor that releases the
allocated memory; thus, the prorgammer does not need to worry about
keeping track of *when exactly* the pointer is no longer used and
delete it (notice the emphasis on the "exactly" -- if you delete
the pointer before its last use, then you get undefined behaviour;
and if you forget to delete it, you get a memory leak).
Does the following code have the possibility of leaking memory?
int f()
{
int * x = new int;
*x = 10;
vector<int> values(200);
delete x;
return 0;
}
If your answer is -- as I would expect it to be -- no, well,
think again... Smart pointers (such as auto_ptr) are notoriously
useful in code that has to deal with exceptions.
If the constructor for vector throws an exception (which it could),
the above code leaks the memory allocated for x.
A solution would be:
int f()
{
try
{
int * x = new int;
// ... (exactly the same as above)
}
catch (...)
{
delete x;
throw;
}
}
Sure, but it's quite messy ... Plus, with real functions that
actually do something, things can easily get complicated enough
that it is hard to keep track of when to delete... Notice, also,
how the above code did not have to worry at all about housekeeping
for the memory management related to the vector -- the class
encapsulates all the messy details. That's the idea with smart
pointers classes: they relieve you from all the messy details.
The following code is perfectly ok in the presence of exceptions:
int f()
{
auto_ptr<int> x (new int);
*x = 10;
vector<int> values(200);
return *x; // HOW ABOUT THIS!!! (isn't it fantastic?!! :-))
}
No, it does not leak memory under any circumstance!!!! Whenever
object x is destroyed, its destructor releases the allocated
memory -- that it is because of an exception, that it is because
a premature return, or because x reached the end of its scope;
in the above code, *x is evaluated still within the lifetime of
x -- after that, when the value of *x is already "cached" and
control is about to leave the function, the object x is destroyed,
and its destructor is invoked -- it will release the allocated
memory.
An exception?? So what? The object is destroyed as a consequence
of the exception being propagated through this function, so the
memory will be cleanly released.
Class auto_ptr also has the advantage (for certain situations --
in certain other situations it would be a disadvantage) that it
keeps track of ownership:
auto_ptr<int> x (new int);
auto_ptr<int> y;
*x = 10;
y = x;
OOPS!! Now both x and y, when destroyed, will release the memory
they're pointing to, causing a double-delete bug?? No. When you
assign y with the value of x, *ownership* of the pointee is
transferred to y, and x is invalidated (hmmmm, red flag!!! So,
it is smart, but not genius, eh? :-)). That means that x will
not release the memory, because it does not hold ownership of
the memory pointed at -- y does. So, y's destructor will release
the memory.
Reference-counting is a technique that is implemented also through
smart pointers -- a different type of smart pointers, such as the
soon-to-be-part-of-the-standard-library shard_ptr. It is also a
smart pointer, in that it provides the semantic and behaviour of
regular (bare) pointers, but it does more, given that now you
have a class that can wrap/encapsulate plenty of extra functionality
needed for whatever particular purpose (reference counting, in this
case)
Reference counting structures are related to smart pointers, but
not the same thing
A reference-counting pointer class *is a* smart pointer (is one
particular type of smart pointers). Smart pointer is a more
general notion.
Carlos
--
.
- Follow-Ups:
- Re: what is the purpose of C++ smart pointer
- From: Wolfgang Draxinger
- Re: what is the purpose of C++ smart pointer
- From: Binary
- Re: what is the purpose of C++ smart pointer
- References:
- what is the purpose of C++ smart pointer
- From: Binary
- Re: what is the purpose of C++ smart pointer
- From: Wolfgang Draxinger
- what is the purpose of C++ smart pointer
- Prev by Date: Re: Problems using gtk on object-oriented attempt
- Next by Date: VARIANT, SAFEARRAY and BSTR implementation on Linux.
- Previous by thread: Re: what is the purpose of C++ smart pointer
- Next by thread: Re: what is the purpose of C++ smart pointer
- Index(es):
Relevant Pages
|
Loading