Get Paid for Working on Projects Matching Your Expertise at Go4Expert's Jobs Board
Go4Expert
Go4Expert RSS Feed

Go Back   Programming and SEO Forum >  Go4Expert > Queries and Discussion > Programming > C-C++

Reply  Copy HTML to Clipboard  Copy BBCode to Clipboard  | More
 
Bookmarks Thread Tools Search this Thread Display Modes
Old 10-12-2007, 08:36 PM   #1
vql
Go4Expert Member
 
Join Date: Sep 2007
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
vql is on a distinguished road

Why the program not crash when access NULL object?


Quote:
#include <stdio.h>
class A {
public:
void PrintMe();
};

void A::PrintMe()
{
printf("Hello World\n");
}

void main()
{
A* p = 0;
p->PrintMe(); // why not crash
}
Please explain why the program is not crash when access to function PrintMe of object NULL. Please explain me clearly. Thanks.
vql is offline   Reply With Quote
Old 10-12-2007, 10:19 PM   #2
Go4Expert Founder
 
shabbir's Avatar
 
Join Date: Jul 2004
Location: On Earth
Posts: 12,744
Thanks: 130
Thanked 294 Times in 228 Posts
Rep Power: 10
shabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud of
Send a message via Yahoo to shabbir

Re: Why the program not crash when access NULL object?


Because you compiler does find that function where p points.
shabbir is offline   Reply With Quote
Old 10-12-2007, 10:46 PM   #3
Team Leader
 
DaWei's Avatar
 
Join Date: Dec 2006
Location: Texan now in Central NY
Posts: 835
Thanks: 0
Thanked 3 Times in 2 Posts
Rep Power: 4
DaWei is on a distinguished road

Re: Why the program not crash when access NULL object?


Because your compiler is not standards compliant (neither is MS) in this respect.

Your use of the return type, "void", for main is also non-compliant.
__________________
DaWei on Pointers Grumpy on C++ Exceptions
Functionality rules; clarity matters. If you can work a little elegance in there, you're stylin'.
DaWei is offline   Reply With Quote
Old 10-13-2007, 03:37 PM   #4
vql
Go4Expert Member
 
Join Date: Sep 2007
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
vql is on a distinguished road

Re: Why the program not crash when access NULL object?


Quote:
Originally Posted by shabbir
Because you compiler does find that function where p points.
I think you are wrong because p is NULL pointer. The function also have a address, where p points to. But compiler can not know p's address -> so compiler also don't know the function's address.
vql is offline   Reply With Quote
Old 10-13-2007, 06:12 PM   #5
Go4Expert Founder
 
shabbir's Avatar
 
Join Date: Jul 2004
Location: On Earth
Posts: 12,744
Thanks: 130
Thanked 294 Times in 228 Posts
Rep Power: 10
shabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud ofshabbir has much to be proud of
Send a message via Yahoo to shabbir

Re: Why the program not crash when access NULL object?


Quote:
Originally Posted by vql
I think you are wrong because p is NULL pointer. The function also have a address, where p points to. But compiler can not know p's address -> so compiler also don't know the function's address.
If you are using MS compiler try using an F11 at the function call and see what happens
shabbir is offline   Reply With Quote
Reply  Copy HTML to Clipboard  Copy BBCode to Clipboard  | More


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Bookmarks

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

 

All times are GMT +5.5. The time now is 01:10 AM.