How does C++ store functions and objects in memory?
c++, function, turbo-c
Solution
Obviously, the code has undefined behavior, i.e., whatever you get is by chance. That said, the system doesn't need to know about the object when calling a non-virtual member function: It can just be called based on the signature. Further, if a member function doesn't need to access a member, it doesn't need really need an object at all and can just run. This is what you observed when the code printed some output. Whether this is how the system is implemented isn't defined, however, i.e., nothing says it works.
When calling a virtual function type system starts off looking at a type information record associated with the object. When calling a virtual function on a `NULL` pointer, no such information exists and attempting to access it probably leads to some sort of crash. Still, it doesn't have to but it does for most system.
BTW, `main()` always returns `int`.
Problem
Lets say we have a class ``` class A { int x; public: void sayHi() { cout<<"Hi"; } }; int main() { A *a=NULL; a->sayHi(); } ``` The above code will compile on Turbo C (where I tested) and print `Hi` as output. I was expecting crash because `a` is `NULL`. More over if I make `sayHi()` function virtual, it says ``` Abnormal temination(Segmentation fault in gcc) ``` I know a lot of it is implementation dependent but if anybody could throw some light on any implementation or just give an overview it would be really nice.