RSS .92| RSS 2.0| ATOM 0.3
  • Home
  • About
  •  

    Auto Pointers

    February 14th, 2010

    Pointers are still a nightmare for most of the C++ beginners. The confusion where to deallocate, deep copy or shallow copy results in memory leaks and occasional program crashes. A possible remedy to this is auto pointers.

    Image courtesy : http://www.usscouts.org
    

    Auto pointers provide some kind of automatic garbage collection. That means the programmer doesn’t have the headache of deleting the pointer, thereby ensuring that there won’t be memory leaks due to his carelessness.

    Unlike normal pointers, there can be only one owner for an auto pointer at a time, though ownership can be transferred.

    An auto pointer can be created using the syntax

    auto_ptr<T> variable name (new  T), where T is the class type to which the pointer belongs.

    An example is

    auto_ptr<int> myInt(new int(10));

    To transfer the ownership, we can simply assign the above to another auto_ptr variable.

    auto_ptr<int> myNewInt = myInt;

    In this case, myNewInt points to the location where 10 is stored and myInt = NULL:

    To assign an auto_ptr to a normal pointer, we can use the auto_ptr method release. Hence, the above functionality can be obtained in the following way also.

    int* myNormalInt = myInt.release();

    On calling release, the auto_ptr is set to NULL where as the memory location pointed by it is not destroyed, but assigned to the new variable.

    To access the memory location pointed by the auto_ptr variable, we can use the method get().

    A small example is described below.

    First we create a simple class

    
    // A simple class
    class CMyClass
    {
    public:
       // Constructor
       CMyClass(int no)
       :m_No(no)
       {
       }
       // Display the member variable
       void Display()
       {
          cout << "No is << " << m_No << "\n";
       }
    private:
       int m_No;
    };
    

    Now various operations using auto_ptr are shown below.

    
    #include "stdafx.h"
    #include <iostream>
    #include <memory>
    #include "assert.h"
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
       // Create an auto_ptr
       auto_ptr<CMyClass> myClass(new CMyClass(10));
       // Assign the auto pointer to a new auto_ptr
       auto_ptr<CMyClass> myNewClass = myClass;
       // myClass will be NULL now
       assert(myClass.get() == NULL);
       // myNewClass now points to the location
       // where myClass pointed to
       myNewClass->Display();
       // myNewClass can be assigned to a normal
       // pointer as below
       CMyClass* myNormalClass = myNewClass.release();
       // myNewClass now points to NULL
       assert(myNewClass.get() == NULL);
       // myNormalClass now points to the location
       // myNewClass pointed to
       myNormalClass->Display();
       // Finally, we can delete myNormalClass.
       // Note that the auto_ptr variables do
       // not need deletion
       delete myNormalClass;
       myNormalClass = NULL;
       return 0;
    }
    

    Cppkid has a new home

    February 13th, 2010

    Image courtesy : http://www.freeprintablecoloringpages.net

    A couple of years ago, cppkid stepped into the world of technical blogging with free space provided generously by Wordpress with an idea to share what he was learning. He has now got his own home in the cyber world under the name techievibes.

    He is always grateful to the support he has got and he hopes that it will continue in the future also. He once again expresses his sincere thanks to all who have provided support (through both criticism and praise).

    See you with next post soon…..


    How To Round Off A Number

    February 18th, 2009

    Sometimes, it is needed to round off a number to some decimal digits.  Again stringstream comes to help us with the aid of iomanip function setprecision. Following is a sample function to round off a number to a fixed digits.

    double Round(const double value, const int digits)
    {
       stringstream stream;
       // Store the number with required no. of decimal
       // places to stream
       stream << setprecision(digits) << value;
       // Convert stream to number
       double roundedValue = 0.0;
       stream >> roundedValue;
       return roundedValue;
    }
    

    The header files sstream and iomanip are needed for the above.

    Please note that the variable value holds the number with original no. of decimal places and digits is the total number of digits needed (including the non-decimal digits) after rounding has taken place.

    Round(10.37665, 3) gives 10.4 and Round(10.3745, 4) gives 10.37.

    Note

    If you want to store the number in scientific notation in stream, you can do

    stream <<  scientific;

    For fixed point format, do

    stream <<  fixed;