Archives
D Programming
DD.gnu digitalmars.D digitalmars.D.bugs digitalmars.D.dtl digitalmars.D.dwt digitalmars.D.announce digitalmars.D.learn digitalmars.D.debugger C/C++ Programming
c++c++.announce c++.atl c++.beta c++.chat c++.command-line c++.dos c++.dos.16-bits c++.dos.32-bits c++.idde c++.mfc c++.rtl c++.stl c++.stl.hp c++.stl.port c++.stl.sgi c++.stlsoft c++.windows c++.windows.16-bits c++.windows.32-bits c++.wxwindows digitalmars.empire digitalmars.DMDScript electronics |
c++ - strange behaviour (vs cpp compiler bug !?). call on null pointer succeeds.
Hi, This behaviour made me spend some time trying to figure out what's happening in the project Im working on. But putting it in a simple test it has the same behaviour. You have 2 simple classes. One has a public method. The other one has a member data as a pointer to the first class. I initialize on the ctor the pointer to NULL. Even so, when calling the first class' method through the pointer, it works ! I cant believe it works. If you look at the assembly code, it looks like the method is found without a problem, nothing weird. I tested on 3 computers and it works (Windows XP SP2). Compiler: Visual Studio 2003 and 2005. // UnInstantiated.h #pragma once class CUnInstantiated { public: CUnInstantiated(void) { m_nValue = 1; } void Boo() { // this will work printf("Boo"); // this will do access violation //printf("Boo ! %d", m_nValue); } int m_nValue; }; // user.h #pragma once #include "UnInstantiated.h" class CUser { public: CUser(void) { pNULLPointer = NULL; } void Do() { pNULLPointer->Boo(); } private: CUnInstantiated* pNULLPointer; }; // main.cpp #include "stdafx.h" #include "User.h" int _tmain(int argc, _TCHAR* argv[]) { CUser user; user.Do(); return 0; } Jan 09 2007
andrew skrev:Hi, This behaviour made me spend some time trying to figure out what's happening in the project Im working on. But putting it in a simple test it has the same behaviour. You have 2 simple classes. One has a public method. The other one has a member data as a pointer to the first class. I initialize on the ctor the pointer to NULL. Even so, when calling the first class' method through the pointer, it works ! I cant believe it works. If you look at the assembly code, it looks like the method is found without a problem, nothing weird. Jan 09 2007
I agree with you, its not reliable. I know that method are just like any other functions internally but I dont agree 100% that 'there is no reason why it should not work'. What if Boo( ) would do something which i dont expect to be done if the CUnInstantiated is not instantiated (from the logical point of view i mean). Calls on an invalid pointer (bad memory) are expected NOT to be working. Again, I do know that methods are like any other functions but its very misleading. This means ANY function from CUnInstantiated will work (as long it does not use members of CUnInstantiated of course): pNULLPointer->Boo(); pNULLPointer->Boo1(); .... Like you said its not reliable of course. But the fact that it is working today on my machines will make the bug get away. thank you Jan 09 2007
"andrew" <user earth.com> wrote in message news:eo25ai$1nc2$1 digitaldaemon.com...I agree with you, its not reliable. I know that method are just like any other functions internally but I dont agree 100% that 'there is no reason why it should not work'. What if Boo( ) would do something which i dont expect to be done if the CUnInstantiated is not instantiated (from the logical point of view i Jan 10 2007
|