Thursday, August 30, 2012

Calling a function using NULL object

#include <iostream.h>
#include <conio.h>

  class base 
    {
     int a ;
     public:
     
       void displaySomething()
       {
       cout<< "displaySomething"<<endl ;                   
       } 
  
      void displayMember()
      {
       cout<< a;    
       }
                
      };

  main()
  {
   base * obj = NULL ;
   obj->displaySomething();
   obj->displayMember();
     getch();
     }


Check out the above simple pgm , before running it  .. just think , where the app will crash??
 1. will it crash in obj->displaySomething(); 
 2. obj->displayMember();
 3.  cout<< a;  

run the program 

Are you surprised !!!    

here lesson is , to call the function , we don't need concrete object  (btw , this functions are stored in code segment) .

when we are trying to call "a" which is not available in memory , then app leads to crash   

(Note :  In 3rd party or Native application , we are writing logs at the  beginning and end of the function       for debug purposes . there may be some cases, we will get crash logs , showing only beginning of the function . At this point we are start focusing on the function but null object could be the reason crashing )

However it is a good pgm to know about the data and code segment .

Monday, August 27, 2012

Reversing singly Linked List - Explanation

I have searched some sites to know how they are reversing linked list , yeah I found so many sites but none of them are explained . So , I conclude it  to do in my blog



Below are the working piece of code 

#include <iostream.h>
#include <conio.h>

class Node
{
  public:
      int data ;
      Node * next ;
      Node(int in){data = in; }     // public param cons
   
      };



Node* Reverse(Node * head )
 {
          Node * temp = NULL ;
          Node * np = NULL;
       
        while (head)
          {
           temp = head->next;
           head->next = np;
           np = head ;
           head = temp;    
       
          }
          return np ;
           
 }


  void Display(Node * head )
   {
      while (head)
      {
       printf("%d \t" , head->data);
       head = head-> next ;  
      }
   }



main()
{
   Node * obj1= new  Node(10) ;
   Node * obj2= new  Node(20) ;
   Node * obj3= new  Node(30) ;
   Node * obj4= new  Node(40) ;
 
   obj1->next = obj2 ;
   obj2->next = obj3 ;
   obj3->next = obj4 ;
 
   printf("Before reversing ");  
   Display(obj1);  
     
   Node * head =  Reverse(obj1);
 
   printf("\n After  reversing ");  
   Display(head);
   getch();  
     
}



Lets see the explanation of Important lines 
  before getting in to that , you need two variable
1. temp -> used to hold next element pointer and given back the address to head
2. np    -> Assign it to head->next

lets discuss the code line by line

step1 :        temp = head->next;  
 take the information of next , so that we can play with head->next

step2:        head->next = np ;
 yeah , In the next line we are changing head->next  ( anyway we are having this info in temp   for later use)
initially np  is null , but from the second iteration it has the info of prev element

step3:       np = head ;
yeh , this step does the same what we want in step 2

step4:    head = temp ;

finally head should move to next element . fortunately this info is stored in temp 


thats all dudes , np will become your new head pointer .

 It may not be the great explanation , but it will be used to the beginners .
 


Sunday, August 26, 2012

Template

we are going to discuss about the template 

Template is a skeleton for which we are going to give different body .



where it can be used

one important thing comes in to my mind ... that is logs 

I have written one third party app , also it has one log to write some important logs 

well, the input param could be either int , string  ,double  or float .. I guess , for logs we are not using more than this primitive 

Code

template <class T>

void log_function(T in , const char* tag)
 {
 cout<<tag << "\t" << in ;
}

see , how good it is , otherwise we are going to write a overload function which will take the different data type 


by the way , when call is made 

Ex:    log_function(15 ,"TAG LINE");
         log_function("hello world" ,"TAG LINE");
         log_function(15.45 ,"TAG LINE"); 

compiler generates function for each data type . So, it internally does the overloading :) 

good news is , we can avoid manual mistakes 

STACK

STACK is the one of the important data structure in the computer science world .
every one should know , how stack DS is implemented in program .

Lets see the below code


int add(int a ,int b)
{
  return a+b ;
}

add(3,5) ;  


when add is getting called , we should know ,what is happening internally and what are the information is storing in stack .


If you realize this , then you can understand the risk of using recursive function :)

well, from the above , you can infer , why thread needs separate Stack .

Stack is very simple but from that you can learn more


by the way , Don't think  Stack  is different memory . It is same as Heap , Both memory (Heap and Stack ) taken from RAM .
Small amount of memory is separated and it is surrounded by the Stack DS .  




Tuesday, August 14, 2012

Singleton Pattern

Singleton is one of the important design pattern in software development

Main objective of this pattern is , to create single instance of the  class  . Lets see how we are achieving that



class singleton
{
 private
   singleton() {}  

public:
  static singleton* CreateInstance() {

     if(!instance )
     {
     instance  = new singleton() ;
      }
   
    return instance  ;
   }

static singleton* instance ;

}


Important things you must know :

 1. Why Constructor is private
     -> So that , other class (outside of this class )  can't create the object .
                   

2. Why we need static function  ie  static singleton* CreateInstance()
   ->  From first point , it is clear ,we can't create object of the class . So, static is the only way we can call function with out creating object of the class

3.  Why member is static
   ->  As it is used inside the static function , It should be static
    just google it , you will get more details about it


Advantage :
 Using this pattern you are maintaining only one instance of this class

Setback :
  1. This object is alive as long as application is alive