Solved

Reference to no object does not produce any run time error  (MSVS2010 C++)

Posted on 2012-03-30
6
378 Views
Last Modified: 2012-03-31
//my program wants to get a reference to an orange.
orange& myOrangeRef=theApp.findOrangeByID(id); //no oranges yet
myOrangeRef.someFunc();//succeeds, but WHY? //No object exists why does call succeed

CODE
//caller
orange& myOrangeRef=theApp.findOrangeByID(id);


//called function
orange& CProject2App::findOrangeByID(integer id)
{
        .....
        .....
      return *pOrange;  //pOrange is NULL, no error in CALLEE
}
0
Comment
Question by:supportoranges
  • 3
  • 2
6 Comments
 
LVL 22

Accepted Solution

by:
ambience earned 250 total points
ID: 37791635
When you return a reference to an object, the compiler actually returns the address of the object which is 0 (NULL to be exact). And it could be any garbage value, and there wont be a runtime error unless that address gets used in some way. Even if you call a non-static non-virtual method on the returned garbage object it will work, as long as no data-member is accessed or a virtual method is called e.g.

((orange*)0)->someNonVirtualMethodThatDoesntTouchDataMembersEitherAndDoesNotCrash()

Since you are returning reference, the copy constructor or assignment operator wont get called either. Even if they get called, as long as the class is empty it just wont touch the address (the this pointer).

In short, its OK to have an invalid "this" pointer or address (and im not saying its a good practice) as long as you dont get to use it. Virtual methods are one example, where this pointer gets used in the code generated by almost all compilers.
0
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 250 total points
ID: 37791640
As you said, the address of myOrangeRef is NULL and so no object was allocated.
But your function someFunc() is either static, in which case no object is needed; or, it is not referring to any myOrangeRef class data member. If someFunc() were referring to a myOrangeRef class data member (which never got instantiated), then you would get a run-time fault (probably a segfault).
0
 
LVL 32

Expert Comment

by:phoffric
ID: 37791648
typo - Should be " or, it is not referring to any orange class data member.
(looks like I was typing while ambience was submitting)
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 32

Expert Comment

by:phoffric
ID: 37791674
Although this is the first time I've seen a reference having a NULL value, (so I'm guessing this isn't standard or good practice), if you actually did have a need to pass this exceptional NULL case using your constructs, then the caller needs to test for this condition, as follows:

     if( &myOrangeRef == NULL ) {
       // Handle the exceptional NULL case condition here
     }
     else {
       // normal processing here
    }
0
 
LVL 1

Author Comment

by:supportoranges
ID: 37791694
Thanks for contributing -- I feel like I know so much more about references now!
0
 
LVL 1

Author Comment

by:supportoranges
ID: 37791702
You are both right - I never accessed data members so it never crashed and I will redesign my construct.  Thank you.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now