Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C++ Programming

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.

(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.

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.

On Wed, 06 Jun 2007 16:23:45 -0000 in comp.lang.c++, Deep
<mail2sukhd@gmail.com> wrote,

>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

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.

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc