Sunday, December 11, 2011

member functions are given by compiler in c++

Following member functions are given by compiler if we are not defined in the class..

1) default constructor
2) default destructor
3) copy constructor
4) assignment operator
5) address operator

Wednesday, November 23, 2011

Things to be taken care of in defination of assignment operator

  • Deep copy/shallow copy
  • Guard against self assignment in the assignment operator.
  • Strong exception safety in the assignment operator. (acquire new resources before deleting old)
  • Call base class's assignment operator if needed.

Final Class in C++

A class in Java is said to be final class if it can't be inherited by any other class. In C++, language has no built in feature to implement this. This has to be implemented by the developer.
  • By making the destructor private 
    • In this case, the object can only be created in heap by some static function defined in the class. Similarly a function must be implemented for destruction of object.
    • If we create object on stack, stack unwinding needs the destructor to be accessible. Hence compiler won't allow this.
    • Let's look at the following code.
    • Here no class can inherit F as the destructor is private. During child class object destruction, parent class destructor can't be called. Because parent class destructor is private.

  • By friend class
    • In this case, the object can be created on stack. 
    • Here we will need a dummy class whose destructor is private. The Final class must be a friend of this class and the Final class will inherit from dummy class.
    • We can create the object of Final class. As Final class is the friend of dummy class, it will have access to it's destructor. 
    • But if any class(Child) inherits Final Class, then it won't compile because destructor of temp is not accessible inside Child class.
    • Let's look at the following code

Wednesday, October 12, 2011

Implementation of Binary Search Tree

Monday, October 3, 2011

Array vs linked list

A few reasons why Array is better
  • In array, we the provision of random access. But in linked list, the nodes can only be sequentially accessed. 
  • Better locality - Whenever the OS brings data into memory, it loads a whole page. Hence less page fault occurs. With linked list, different parts of the linked list are stored at different parts of memory. Hence branch Prediction doesn't work as well with linked list. This causes the pipeline to be flushed more often which result in poor performance.
  • In linked list, the extra storage needed for references, which often makes them impractical for lists of small data items such as characters or boolean values. It can also be slow, and with a naïve allocator, wasteful, to allocate memory separately for each new element.

A few reasons why Linked List is better
  • It's easier to store data of different sizes in a linked list. An array assumes every element is exactly the same size.
  • It's easier for a linked list to grow organically. An array's size needs to be known ahead of time, or re-created when it needs to grow.
  • Shuffling a linked list is just a matter of changing what points to what. Shuffling an array is more complicated and/or takes more memory.
  • As long as your iterations all happen in a "foreach" context, you don't lose any performance in iteration.

Saturday, September 24, 2011

new instead of malloc

While searching I found a very naive, but useful thing about new/malloc.

Why should we use new instead of malloc ?

  1. New and Delete makes sure that constructors and sestructors are called but not the case with malloc and free functions of C. 
  2. Pointer conversion safety: malloc() returns a void* which isn't safe. new returns a pointer of the right type. 
  3. new is a Operator: new is an operator that can be overloaded for better memory management by a class, while malloc() is not an operaotor. 
  4. new operator computes the size of object automatically whereas malloc cannot.
  5. It is possible to initialize the object while allocating memory with new. 
  6. Its possible to construct an object over an already allocated memory using another version of 'new' operator known as 'placement new' operator. While with malloc() it is not possible.

Friday, September 16, 2011

Nth last element in Linked List (C++)

The following code shows how to get nth last element in a linked list. It uses a queue to hold the temporary elements.

1>Make a queue of length N
2>Traverse the linked list from root
3>While traversing, en-queue first N elements
4>After this for en-queuing every element, de-queue another element.
5>When we reach the end of linked list, the last de-queued element will be the Nth last element.


Tuesday, September 13, 2011

CountDown Latch to measure sorting time (java)

The following code uses CountDown Latch to measure the performance of sorting example. The sorting method sorts n array of random numbers in separate threads.

Monday, August 15, 2011


Look at the following code.

The above code will work perfectly if the "do some stuffs" section doesn't throw any unhandled exception. If some exception is thrown, then the object will be created in the heap and won't be deleted till the program termination.

Hence, object creation in heap is always dangerous than object creation in stack. We can avoid the above situation by using auto_ptr templatised class in STL.

Automatic Pointer(auto_ptr) class is a limited garbage collection facility which takes responsibility of object destruction when the auto_ptr goes out of scope.

Auto_ptr takes the responsibility of the destruction of the object. Hence if same auto_ptr points to another object, the previous object is destroyed properly.

Example :-

Output :-

If the responsibility is transferred from 1st pointer to the second pointer(by assignment operator), then 1st pointer is set to NULL. Hence, no two auto pointer points to the same object.

Example :-

Output :-

Certain Links that might prove useful