It's undefined behavior, so anything might happen.
It's undefined behavior, so anything might happen. A possible result would be that it just prints "fun" since the method doesn't access any member variables of the object it is called on (the memory where the object supposedly lives doesn't need to be accessed, so access violations don't necessarily occur).
– Kamal Mar 28 '10 at 16:08 2 @Kamal If the function is not virtual, then it gets statically linked. No need for a table.No need for the object pointer. – Frank Krueger Mar 28 '10 at 16:13 2 @Kamal The compiler could use static information (it knows the pointer is of type A) to find the function.
The fact that the compiler can do this does mean that it must, and does not prevent this from being undefined behaviour. – anon Mar 28 '10 at 16:13 @Kamal: Who said it must be stored? The compiler can transform the function call into A::fun(a).
– KennyTM Mar 28 '10 at 16:14 @Kamal: The compiler knows the type of the object and the address of the method that should be called at compile time and can insert that address "hard coded". There is no need to look anything up at runtime. (This doesn't mean the compiler has to do it that way, but it could.
) – sth Mar 28 '10 at 16:14.
I have tried multiple times,all the time output is coming "fun" this is because function fun is independent of instance a. While calling a->fun(); a points to 0 so this is undefined behavior but in most of the compilers there should be no crash.
A* a = NULL; emphatically does not result in a constructor call – Dennis Zickefoose Oct 29 at 21:04.
By the standard, this is undefined behavior and therefore a very bad thing. In reality of most programming platforms (across both X86 and several other architectures) this will run fine. Why?
Consider how class functions are implemented in C++. This isn't a virtual function, therefor this can be a static call to a known address. In assembly, we can see this as mov A, 0 mov ecx, A call a__fun since a__fun requires no instance data, even though it receives a null this pointer, nothing will happen.
Still shitty code and any compiler will scream, but it can run.
Others have pointed out many issues - let me just throw in my association for "tricky": (supposed you change Class to class) it depends on how NULL is defined ;-) Evil example: A anotherA(); #define NULL &anotherA; ... OK, as I realize it wouldn't work this way directly, since you must put these two lines between the definition of class A and that of a... but I am fairly sure there is an evil enough way to actually make this technically possible :-(.
NULL is simply 0 and I don't think this qn is that much tricky! – mag Mar 28 '10 at 16:03 @Rajendra well, NULL is #defined as 0 with a preprocessor macro in one of the standard C header files, to be precise... so it can be redefined (which of course should not be done in any serious program). And this just my 2 cents worth anyway - feel free to omit it :-) – Péter Török Mar 28 '10 at 16:09 Knew that!
But this question doesn't have any problem with NULL. My curiosity is why it prints "fun". Can you tell me?
– mag Mar 28 '10 at 16:12 1 If you change it to template struct B { static T t; }; template T B::t; you can say #define NULL (&B::t) xD – Johannes Schaub - litb Mar 28 '10 at 16:15 2 @Johannes - wow. And you needn't even redefine the comma operator ;-) – Péter Török Mar 28 '10 at 19:20.
We can't know what will. Everything can happen, because the program exposes undefined behavior. See Does invoking a member function on a null instance cause undefined behavior?.
It won't compile. There is a typo in the Class keyword, which should be lowercase. Fun() is private.
If you solve those two minor problems, it will run, and print "fun", because: C++ doesn't check whether the pointer is NULL before calling the function fun() is not virtual, so there's no need to refer to a vtable to call fun() fun() never access any member variables in A so it doesn't need to dereference the null this pointer.
1 3. A* a = NULL; a->fun(); is illegal outside of a function body. – sepp2k Mar 28 '10 at 15:51 @sepp2k, good point.
– Ken Bloom Mar 28 '10 at 15:53 But I think we still can bend our minds and sort of understand what he meant – henle Mar 28 '10 at 15:54.
Other than the fact that a C++ class is being used, this kind of problem is language-agnostic.
– Sorush Rabiee Mar 28 '10 at 16:31 @Larry No, it isn't. – anon Mar 28 '10 at 16:49 6 How is it language agnostic. On java, you can precisely say what happens (exception thrown).
– Johannes Schaub - litb Mar 28 '10 at 17:37.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.