Home Forums C Programming Smart pointers…

Viewing 3 reply threads
  • Author
    • #1864

      Hello, its my first itme here,
      can anyone tell me what are the smart pointers in c/c++ and what is their use.

    • #3111

      Smart pointers are C++ objects that simulate simple pointers by implementing operator-> and the unary operator*. In addition to sporting pointer syntax and semantics, smart pointers often perform useful tasks—such as memory management or locking—under the covers, thus freeing the application from carefully managing the lifetime of pointed-to objects.

      The simplest example of a smart pointer is auto_ptr, which is included in the standard C++ library. You can find it in the header , or take a look at Scott Meyers’ auto_ptr implementation. Here is part of auto_ptr’s implementation, to illustrate what it does:

      You can find many readings on smart pointers. I have found two very interesting readings on smart pointers.


    • #3112

      To generalize the type universe of smart pointers, we distinguish three potentially distinct types in a smart pointer:

      The storage type. This is the type of pointee_. By “default”—in regular smart pointers—it is a raw pointer.

      The pointer type. This is the type returned by operator->. It can be different from the storage type if you want to return a proxy object instead of just a pointer. (You will find an example of using proxy objects later in this chapter.)

      The reference type. This is the type returned by operator*.

      It would be useful if SmartPtr supported this generalization in a flexible way. Thus, the three types mentioned here ought to be abstracted in a policy called Storage.

      In conclusion, smart pointers can, and should, generalize their pointee type. To do this, SmartPtr abstracts three types in a Storage policy: the stored type, the pointer type, and the reference type. Not all types necessarily make sense for a given SmartPtr instantiation. Therefore, in rare cases (handles), a policy might disable access to operator-> or operator* or both.

    • #3113

      In computer science, a smart pointer is an abstract data type that simulates a pointer while providing additional features, such as automatic garbage collection or bounds checking. These additional features are intended to reduce bugs caused by the use of pointers while retaining efficiency. Smart pointers typically keep track of the objects they point to for the purpose of memory management.
      For comlete reading goto: http://en.wikipedia.org/wiki/Smart_pointer

Viewing 3 reply threads
  • The forum ‘C Programming’ is closed to new topics and replies.