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 .

No comments:

Post a Comment