473,418 Members | 3,142 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,418 software developers and data experts.

doubt about smart pointers

I'm in doubt about what is smart pointer. so, please give me simple
description about smart pointer and an example of that. I'm just
novice in c++.
regards,
John.

Jun 6 '07 #1
4 1711
(http://ootips.org/yonat/4dev/smart-pointers.html) has some pretty
decent examples on what a smart pointer is. At the most basic level,
they'll save you from having to write "delete" when you're done (or
the pain of finding the memory leak if (or rather *when*) you forget
to). There are also more complex ones that have it so the memory is
deleted when no more pointers point at it anymore, something that can
be very difficult to do with a regular pointer in some situations.
Overall, it's a pointer with a little safety attached to avoid memory
leaks. How exactly it does it depends on how it's set up.

Jun 6 '07 #2
Deep wrote:
I'm in doubt about what is smart pointer. so, please give me simple
description about smart pointer and an example of that. I'm just
novice in c++.
A smart pointer is any object that has the interface of a pointer but is
not a pointer.

See container iterators in the standard library or boost's shared_ptr.

Smart pointers can provide for many different needs and are implemented
in a variety of ways.
Jun 6 '07 #3
On Wed, 06 Jun 2007 16:23:45 -0000 in comp.lang.c++, Deep
<ma***********@gmail.comwrote,
>I'm in doubt about what is smart pointer. so, please give me simple
description about smart pointer and an example of that.
A smart pointer is any class that acts like a pointer (it implements
operator* and/or operator-) and also offers some additional desirable
behavior.

The simple example is std::auto_ptr

A big hairy example is boost::shared_ptr from http://boost.org
Jun 6 '07 #4
Deep wrote:
I'm in doubt about what is smart pointer. so, please give me simple
description about smart pointer and an example of that. I'm just
novice in c++.
A smart pointer is basically a type (usually a template type) that
behaves like a pointer but restricts what you can do with a pointer.

Many smart pointers adopt the RAII idiom (google it) and help make you
code exception safe.

There are many different flavours - std::auto_ptr is the only currently
standard one (iirc) and std::shared_ptr is coming soon in the next
revision of the standard which implements a "reference counted" lifetime
management.

There are others - Austria C++ and Loki just to name a couple.

The amount of code in a smart pointer is rather small but the order of
operations implementing a smart pointer can be rather tricky. You need
to deal with things like self assignment and self deletion, thread
safety etc. If you choose to use smart pointers, regular pointers
almost disappear in your code.

Memory leaks usually disappear as well. I've been using smart pointers
now for about 7 years. Only in the last 5 have compilers really been up
to scratch when it comes to supporting them well enough.

Download one of the smart pointer packages and give it a try.
Jun 7 '07 #5

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

Similar topics

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...
27
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...
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...
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,...
33
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...
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...
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
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...
0
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...
0
tracyyun
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...

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.