473,379 Members | 1,253 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

What's a smart pointer?

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
Jul 22 '05 #1
5 1716

"Gernot Frisch" <Me@Privacy.net> wrote in message
news:2r*************@uni-berlin.de...
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
Jul 22 '05 #2
PKH

"Sharad Kala" <no******************@yahoo.com> wrote in message
news:2r*************@uni-berlin.de...

"Gernot Frisch" <Me@Privacy.net> wrote in message
news:2r*************@uni-berlin.de...
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



Jul 22 '05 #3

"Ioannis Vranos" <iv*@guesswh.at.grad.com> wrote in message
news:ci***********@ulysses.noc.ntua.gr...
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.
Jul 22 '05 #4
PKH

"Method Man" <a@b.c> wrote in message
news:XY****************@read1.cgocable.net...

"Ioannis Vranos" <iv*@guesswh.at.grad.com> wrote in message
news:ci***********@ulysses.noc.ntua.gr...
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


Jul 22 '05 #5

"PKH" <no************@online.no> wrote in message
news:X9********************@news2.e.nsc.no...


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


Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Vijayaraghavan Kalyanapasupathy | last post by:
Hello All, I just learnt about smart pointers. Given the advantages of using smart-pointers, as a designer of say a List object, I can inhibit creation of the actual objects by making its...
9
by: christopher diggins | last post by:
I would like to survey how widespread the usage of smart pointers in C++ code is today. Any anecdotal experience about the frequency of usage of smart pointer for dynamic allocation in your own...
24
by: Christopher Benson-Manica | last post by:
Is there anything wrong with my attempt (below) at implementing something resembling a smart pointer? template < class T > class SmartPointer { private: T *t; public:
8
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...
6
by: zl2k | last post by:
hi, When I considered about preventing memory leaking, the method came up to my mind is using boost smart pointer if possible (use stl::vector instead of type, use smart pointer whenever declare...
92
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers,...
5
by: Bhan | last post by:
what r smart pointers?
54
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining...
13
by: Phil Bouchard | last post by:
I am currently writting a smart pointer which is reasonnably stable and I decided supporting allocators for completion because of its increase in efficiency when the same pool used by containers is...
50
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): ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.