Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2012-03-30
6
Medium Priority
?
391 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 22

Accepted Solution

by:
ambience earned 1000 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 1000 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

636 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