Meyer's Singleton pattern

Singleton pattern is my all time favorite. Some people call it anti-pattern as they use it even when it is not actually required. Today, I was reading about singleton pattern and found so many interesting things about it.

static data + static functions != singleton pattern

Static class (a class that contains only static data and functions) and singleton pattern are not same. In some cases it may seem same, but with static classes there are some problems e.g.

  1. You can not extend its functionality by inheritance since static functions can not be virtual.
  2. Initialization and clean up is very difficult in static classes; since there is no central point of initialization or clean up; i.e. data is not bound with constructor or destructor as the whole data is static.

Singleton design pattern takes care of this along with focusing on the fact that there is a single object in the execution of program.

Given below is one design variant of singleton design pattern :

class singleton {
     singleton *pinst_;
    singleton() {};
    singleton(const singelton&) ;
   operator =(const singleton&);
public:
   static singleton* Instance() {
      if(pinst_ == NULL)
         pinst_ = new singleton();
      return pinst_;
   }
}; 

Since all constructors are private, user is not allowed to create its instance. Hence, uniqueness is enforced at compile time. If Instance function is never called, no object is created at all. Instance of singleton is created when Instance is called first time. This optimization is useful, if size of class is too big to be very expensive, but what if singleton object is not too big.

In that case, one can keep static object of  singleton class instead of pointer as follow : 

Another design Invariant :
class singleton {
     singleton inst_;
    singleton() {};
    singleton(const singelton&) ;
   operator =(const singleton&);
public:
   static singleton* Instance() {
      return &inst_; // return its reference
   }
}; 
in implementation file 
singleton singleton::inst_; // initializing static object. 
This is not a good solution although everything is same in second invariant as well except that in 2nd approach there is static object and in first there is pointer. 

In second approach, inst_ is initialized dynamically at runtime and in first approach it happens statically (It is a type without constructor initialized with compile time constant). .
The compiler performs static initialization before the very first assembly statement gets executed. But compiler does not define the order of initialization for dynamically initialized objects found in different translation unit (A compilable source file).
int global = singleton::Instance().do_something();
depending on the order chosen by compiler to initialize global and inst_ singleton::Instance() may return an object that has not been even constructed yet. 


Destroying singleton : As we discussed above, first approach is more reliable than the second one. But it has problem that when and how to destroy the static singleton object. Like there is a function Instance(), we can make a public function destroy() that will call destructor  but one has to be very careful that nobody access the object after it has been destroyed. we would not have to face this problem if we have used second approach but definitely that is more dangerous. Scott meyer came up with another approach. Therefore, people refer it as Meyer's singleton.

singleton& singleton::Instance {
   static singleton obj; //function static object
   return obj;
}

Function static object is initialized when control flow hits this function first time. Primitive static variables are initialized with compile time constants. e.g
int func() {
  static int x=100;
  return x++;
}

In this case, x is initialized to 100 before any code in the program is executed most likely at the load time. When the static variable is not compile time constant or an object with a constructor it is initialized when the program hits for first time.  

A pseudo C++ code generated by compiler : 

singleton& singleton::Instance() {
// functions generated by compiler  extern void __ConstructorSingleton(void *memory);  extern void __DestroySingleton();// objects created by compiler
  static bool __initialized  = false;  static char __buffer[sizeof(singleton)];  if(! __initialized) {     __ConstructorSingleton(__buffer);     atexit(__DestroySingleton);    __initialized = true;
  }  return *reinterpret_cast<singleton*>(__buffer); }

Main part here is atexit function, which is provided by standard c library. It allows register function to be called automatically during program's exit. 

Each call to atexit function pushes its parameter on a private stack maintained by C runtime library. During application's exit, these functions are called.

P.S
In subsequent  posts we will disucss about following things in more details :

  1. The compiler performs static initialization before the very first assembly statement gets executed. But compiler does not define the order of initialization for dynamically initialized objects found in different translation unit(A compilable source file). 
  2. atexit function.. problem associated with it.
  3. Renterpret cast

Comparison between Array, Linked List and Vector

Arrays, linked lists and vectors are used as a storage for multiple element components. Each of these has its own merits and demerits in terms of memory occupied, speed of traversal and complexity. In this post, we will try to have a brief but concise comparison between the three:

ArrayArray is a data structure that can store a fixed number of elements of of similar type at contiguous memory locations.
In C, the most simple way to declare an array is as shown below:
int a[10];
The elements of an array are stored at contiguous locations. For example, let us say, if the array is an array of integers of 4-bit each and first element is stored at location 100, the subsequent elements will be stored at locations 104, 108 and so on.
Figure 1: Storage of array elements
This statement will create an array of 10 integer elements located at contiguous locations as can be seen in figure 1 alongside. Let us say, first element is located at 100th location in memory; then, second element will be at 104th location assuming the size of integer as 4 (although size of integer depends on machine. On 64 bit machine, size of integer will be 8 byte ) and third element will be located at  108 and so on.

NOTE: The given example of storage of array in memory does not represent real memory locations. This is just for the sake of understanding. 

Since, array elements are located at contiguous memory locations, rate to access any element in array is constant O(1). e.g. nth element can be accessed as :

    base address + (n-1)*size of element

e.g. 3rd element is present at 100 + 2*4 = 108th address.

Advantages of array : As explained, array has high access rate of order O(1) i.e. constant access rate. The access time for a very large array can be reasonably small.

Disadvantages : 1) Since size of array is constant. It has to be pre-determined, as that much amount of memory has to be reserved which has to be contiguous locations, which can lead to memory wastage.
2) If memory is fragmented into smaller chunks then array creation may fail, because array elements are stored at contiguous locations. e.g. let us say, there is a space of 1024 bytes in memory but memory is fragmented into 2 chunks of 512 bytes. Then, maximum size allowed of an array will be 512 although total available space is 1024 bytes. 
3) Insertion and deletion in the middle of array is a costly operation because elements to the right of required position has to be moved. 

Linked List : Due to the disadvantages of array related to insertion and deletion time, Linked list came into existence. Linked list is a linear data structure that can store collection of data elements. Number of elements to be stored in linked list need not to be constant. It can be dynamic. Elements in linked list need not to be stored at contiguous locations. Since, elements are not kept at contiguous locations, each element contain the location (address) of next element.

Hence, data elements in Linked list consist of two parts:

  1. Data and
  2. Address of next element. 
The structure representing an element in a linked list can be defined in C as shown below
struct link_list {    int data;    struct link_list *next; }; 
A linked list consists of two parts: data and address of next element.
Figure 2: Linked list representation
Advantages of Linked List : 1) Size of linked list need not to be constant. It can be dynamically decided at the run time.
2) Insertion and deletion in the middle of linked list is of order of 1, O(1) i.e it is constant. It just requires changing the pointers.e.g If you want to add an element  at 3rd position i.e. after 200, 2nd node will point to location 250 instead of 300 and new location 250 will point to 300.

Disadvantages : 1) Access rate is lesser than that of array. Complexity of accessing element is of order of n O(n) where n is number of elements in linked list because one has to traverse through all the element to access last element.
2) Extra memory cost. An element in a list does not contain only data, but pointer to next element as well.   If data is some huge object say 128 bytes, then pointer of size 4 bytes is not a big overhead but if the data is itself of size 4 bytes like an integer then size of each element will be doubled.

Vector : Vector is a data structure which provides advantages of both linked list and array. Major advantage of array is higher access rate and that of linked list is dynamic size. A vector is a hybrid of these two. 
Vector data structure internally uses an array of some user defined size let us say N. When the user tries to put N+1th element in vector following operations takes place:
  1.   It internally creates a new array of double size i.e. 2N 
  2.  Copies the element of previous array into newer one, 
  3.  Destroy the previous array 
  4.  New array comes in place of previous one. 
From this, we can see that extending size of vector is a heavy operation. Hence there is always a trade off between speed and memory. Because, if you choose smaller size initially; then, you are making sure that memory is not wasted. But as soon as your size reaches threshold and new element is to be inserted, above described operation takes place which slows down your activity. On the other hand, if you choose initially larger size, one can get rid of above operations. But then, memory may get wasted. In such cases where we over-estimate size, there is no difference between an array of max size and vector.

So, its a tricky part to choose initial size of vector. You have to choose wisely which depends upon application to application.If you don't mention the size of vector while construction then compiler chooses a default size that I guess depend upon library implementation.

There are so many other linear data structures like skip list, deque etc that tries to optimize the performance and tries to make use of advantages provided by linked list and array.

Also read: