In my opinion following program should be crashed but its not only working but showing the right result("derv is called").
#include <iostream>
using namespace std;
class base 
{
 public:
  virtual ~base(){}
};
class derv: public base 
{
 public:
   void f() { cout << "derv is called" <<endl;}
};
int main() {
  base* p = new base();
  derv *d1 = dynamic_cast<derv*>(p);
 // Since p point to base , so d1 should return nullptr 
 //calling any function using d1, should fail/crash 
 //but why the following line is working ?? 
 d1->f();
}
Sorry I forgot to add few lines in my previous post: If I add a single data member and try to access it, gives me segmentation fault, which I think is the correct behavior. My Question is that why accessing data member changes the behavior ? When variable is not get accessed , calling "f()" function is successful while the same function "f()" gives segmentation fault when accessing with the data member? Is it the undefined behavior?
class derv: public base 
{
 public:
  int x = 0 ; // Added new member,  c++11
  void f() { cout << "derv is called " << x << endl;} //access it here
};
 
     
    