| | |
Null Pointer? will this code work
![]() |
•
•
•
•
Originally Posted by ankitrastogi82
As null pointer points to nothing. So, How does the following code works
#include <stdio.h>
class cb
{
public:
void HelloB()
{
printf("Calling cb\n");
};//This semicolon should not be here...
};
int main(void)
{
cb* pb = 0;
pb->HelloB(); // should it crash here ?
return 0;
}
Whereas crashing of program is concern(it won't..no reason)...it doesn't matter if u initailise it will 0.....member function will be called depending on the type of the pointer
You can even write
C Syntax (Toggle Plain Text)
cb* pb=(cb*)10;
•
•
•
•
Originally Posted by ankitrastogi82
but it is null ptr and null ptr cant be dereferenced .
•
•
•
•
Originally Posted by ankitrastogi82
Also, they cant be used for calling functions.
•
•
•
•
Originally Posted by ankitrastogi82
A null pointer is known not to point to any object or function.It is not the initialization, it is saying that ptr is null.
Example
C Syntax (Toggle Plain Text)
cb obj; cb* pb = 0; pb->somememberfunction(); pb=&obj; pb->somememberfunction();
it will compile, but dereferencing a null pointer like that will often (but maybe not always) crash the program -- it's undefined behavior. In the case of c++ classes, the function will be called with an invalid "this" pointer and any attempt to reference or use class objects will also result in undefined behavior. So "it works" is all relative to this undefined behavior, and won't "work" very long!
Sorry about that...yes it does derefrence the null pointer.....as far as working of code is concerned...what you are doing is wrong
Almost always when a program does something wrong like this, the C++ standard does NOT says that the program must crash, its does NOT say that the program must produce an error message, what it says is that the program has UNDEFINED BEHAVIOUR.
UNDEFINED BEHAIOUR means exactly what it says, anything could happen,
including the program working. If you ran this program on a different
computer, or with a different compiler or even on a different day of the
week you might get different behaviour.
Most compilers will not crash untill you access a member variable
within the function.
In other Words,
Since the member function in this case does not use its "this"
pointer for anything.....therefore it will work most of the time
Almost always when a program does something wrong like this, the C++ standard does NOT says that the program must crash, its does NOT say that the program must produce an error message, what it says is that the program has UNDEFINED BEHAVIOUR.
UNDEFINED BEHAIOUR means exactly what it says, anything could happen,
including the program working. If you ran this program on a different
computer, or with a different compiler or even on a different day of the
week you might get different behaviour.
Most compilers will not crash untill you access a member variable
within the function.
In other Words,
Since the member function in this case does not use its "this"
pointer for anything.....therefore it will work most of the time
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: c program to extract system info
- Next Thread: C Sockets
| Thread Tools | Search this Thread |
* ansi api array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile createcopyoffile createprocess() directory dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux hardware highest histogram homework i/o ide inches infiniteloop initialization input intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft mysql oddnumber open opendocumentformat openwebfoundation pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test testautomation threads unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi






