Null Pointer? will this code work

Reply

Join Date: Aug 2005
Posts: 11
Reputation: ankitrastogi82 is an unknown quantity at this point 
Solved Threads: 0
ankitrastogi82 ankitrastogi82 is offline Offline
Newbie Poster

Null Pointer? will this code work

 
0
  #1
Oct 7th, 2005
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");
};
};

int main(void)
{
cb* pb = 0;
pb->HelloB(); // should it crash here ?
return 0;
}
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Null Pointer? will this code work

 
0
  #2
Oct 7th, 2005
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
  1. cb* pb=(cb*)10;
//although it doesn't make any sense..still the code wil will work
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 11
Reputation: ankitrastogi82 is an unknown quantity at this point 
Solved Threads: 0
ankitrastogi82 ankitrastogi82 is offline Offline
Newbie Poster

Re: Null Pointer? will this code work

 
0
  #3
Oct 7th, 2005
Sunny,
but it is null ptr and null ptr cant be dereferenced . Also, they cant be used for calling functions. 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.
Now look into the problem again.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Null Pointer? will this code work

 
0
  #4
Oct 7th, 2005
Originally Posted by ankitrastogi82
but it is null ptr and null ptr cant be dereferenced .
Neither in your program nor the example which i gave you derefrenced null pointer.


Originally Posted by ankitrastogi82
Also, they cant be used for calling functions.
I guess here i don't need to prove anything as it does call the member function...try running it and check it out

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.
It is a pointer of type class cp..which can be passed null pointer or address of any other object.

Example
  1. cb obj;
  2. cb* pb = 0;
  3. pb->somememberfunction();
  4. pb=&obj;
  5. pb->somememberfunction();
Both will work
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,407
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1468
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Null Pointer? will this code work

 
0
  #5
Oct 7th, 2005
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!
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 16
Reputation: drock9975 is an unknown quantity at this point 
Solved Threads: 1
drock9975 drock9975 is offline Offline
Newbie Poster

Re: Null Pointer? will this code work

 
0
  #6
Oct 8th, 2005
Originally Posted by sunnypalsingh
Neither in your program nor the example which i gave you derefrenced null pointer.
actually you do dereference the null pointer...

PT* ptr = 0;
ptr->whatever();

is the same as

PT* ptr = 0;

(*ptr).whatever();
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Null Pointer? will this code work

 
0
  #7
Oct 9th, 2005
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC