Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > What's a smart pointer?

Reply
Thread Tools

What's a smart pointer?

 
 
Gernot Frisch
Guest
Posts: n/a
 
      09-21-2004
and what for would I need it?

--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com


 
Reply With Quote
 
 
 
 
Sharad Kala
Guest
Posts: n/a
 
      09-21-2004

"Gernot Frisch" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> and what for would I need it?


It is a simple wrapper around a regular pointer. Typically operator * and ->
are overloaded in such a class. One very simple example would be like -
template <class T>
class SPtr
{
T* ptr;
public:
SPtr(T* p = 0) : ptr(p) {}
~SPtr() {delete ptr;}
T& operator*() {return *ptr;}
T* operator->() {return ptr;}
// ...
};


They give feel like raw (or dumb) pointers but one can give semantics to
- Their construction and destruction
- Copying and assignment
- Dereferencing etc

The topic can be best described only by a good text.
Some good references to smart pointers are
- More effective C++ (Meyers)
- Modern C++ design (Alexandrescu)
- Boost smart pointers

Sharad


 
Reply With Quote
 
 
 
 
PKH
Guest
Posts: n/a
 
      09-21-2004

"Sharad Kala" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "Gernot Frisch" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> and what for would I need it?

>
> It is a simple wrapper around a regular pointer. Typically operator *
> and ->
> are overloaded in such a class. One very simple example would be like -
> template <class T>
> class SPtr
> {
> T* ptr;
> public:
> SPtr(T* p = 0) : ptr(p) {}
> ~SPtr() {delete ptr;}
> T& operator*() {return *ptr;}
> T* operator->() {return ptr;}
> // ...
> };
>
>
> They give feel like raw (or dumb) pointers but one can give semantics to
> - Their construction and destruction
> - Copying and assignment
> - Dereferencing etc
>
> The topic can be best described only by a good text.
> Some good references to smart pointers are
> - More effective C++ (Meyers)
> - Modern C++ design (Alexandrescu)
> - Boost smart pointers
>
> Sharad
>


They are very useful if you integrate them with the delete-operator, so that
when deleting memory, all smartpointers that point to that memory will be
invalidated by setting their object-pointers to NULL.
By using a hashtable for memory locations with smartpointers, the
performance hit is low.

PKH







 
Reply With Quote
 
Method Man
Guest
Posts: n/a
 
      09-21-2004

"PKH" <(E-Mail Removed)> wrote in message
news:hqU3d.10665$g%(E-Mail Removed)...
>
> "Sharad Kala" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> >
> > "Gernot Frisch" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> >> and what for would I need it?

> >
> > It is a simple wrapper around a regular pointer. Typically operator *
> > and ->
> > are overloaded in such a class. One very simple example would be like -
> > template <class T>
> > class SPtr
> > {
> > T* ptr;
> > public:
> > SPtr(T* p = 0) : ptr(p) {}
> > ~SPtr() {delete ptr;}
> > T& operator*() {return *ptr;}
> > T* operator->() {return ptr;}
> > // ...
> > };
> >
> >
> > They give feel like raw (or dumb) pointers but one can give semantics to
> > - Their construction and destruction
> > - Copying and assignment
> > - Dereferencing etc
> >
> > The topic can be best described only by a good text.
> > Some good references to smart pointers are
> > - More effective C++ (Meyers)
> > - Modern C++ design (Alexandrescu)
> > - Boost smart pointers
> >
> > Sharad
> >

>
> They are very useful if you integrate them with the delete-operator, so

that
> when deleting memory, all smartpointers that point to that memory will be
> invalidated by setting their object-pointers to NULL.
> By using a hashtable for memory locations with smartpointers, the
> performance hit is low.
>
> PKH
>


So are 'handles' equivalent to smart pointers created/managed by the OS?


 
Reply With Quote
 
Ioannis Vranos
Guest
Posts: n/a
 
      09-21-2004
Method Man wrote:

> So are 'handles' equivalent to smart pointers created/managed by the OS?



When you are saying handles, are you talking about .NET/CLI?



--
Ioannis Vranos

http://www23.brinkster.com/noicys
 
Reply With Quote
 
Method Man
Guest
Posts: n/a
 
      09-22-2004

"Ioannis Vranos" <(E-Mail Removed)> wrote in message
news:ciqe53$1iiv$(E-Mail Removed)...
> Method Man wrote:
>
> > So are 'handles' equivalent to smart pointers created/managed by the OS?

>
>
> When you are saying handles, are you talking about .NET/CLI?
>


I was being very vague, but I guess I was referring to handles in Win32
programming (handle to a window) or COM objects. My knowledge of handles is
very little, but they sound exactly like smart pointers to me.


 
Reply With Quote
 
PKH
Guest
Posts: n/a
 
      09-22-2004

"Method Man" <(E-Mail Removed)> wrote in message
news:XY94d.1150$(E-Mail Removed)...
>
> "Ioannis Vranos" <(E-Mail Removed)> wrote in message
> news:ciqe53$1iiv$(E-Mail Removed)...
>> Method Man wrote:
>>
>> > So are 'handles' equivalent to smart pointers created/managed by the
>> > OS?

>>
>>
>> When you are saying handles, are you talking about .NET/CLI?
>>

>
> I was being very vague, but I guess I was referring to handles in Win32
> programming (handle to a window) or COM objects. My knowledge of handles
> is
> very little, but they sound exactly like smart pointers to me.
>
>


It's not quite the same. Handles in Win32 use reference counting to figure
out how many has pointers to the data and only releases when the reference
count reaches 0.
With smartpointers, an allocation can have many smartpointers pointing to it
when it is deleted. When integrated with delete, all these smartpointers
will be invalidated. I.E. using f.ex pcSmartPtr->GetObject() now returns
NULL for all of them. When using smartpointers you allways have to use a
function like GetObject and check the returnvalue when accessing data.

PKH




 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      09-23-2004

"PKH" <(E-Mail Removed)> wrote in message
news:X9a4d.10819$g%(E-Mail Removed)...
>
>
> It's not quite the same. Handles in Win32 use reference counting to figure
> out how many has pointers to the data and only releases when the reference
> count reaches 0.


While you're correct that that is how handles are *used* in Windows, the
HANDLE data type itself, in C++ (and C) under Windows, is defined as void*.
It's how you obtain, release, and use them that makes the difference. (It
would be a mistake, for example, to use new and delete on one.) But the
object itself is just a pointer to void.

-Howard




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Smart Pointers: Is there something similar to smart pointers in C? MotoK C Programming 59 09-15-2006 07:03 PM
Problems with EAP-TLS with smart cards jr Wireless Networking 1 06-23-2006 09:20 PM
Smart Card Certificate Logon and Smart Card Wireless EAP-TLS erha Wireless Networking 0 05-19-2005 01:40 AM
Is WPS compatible with Smart client as defined in WISPr guidelines nsmurthy Wireless Networking 0 08-13-2004 10:23 AM
trade 64mb smart media for 16mb smart media cards wjva Digital Photography 1 08-20-2003 08:30 PM



Advertisments