?
Solved

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

Posted on 2012-03-30
6
Medium Priority
?
390 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.
Suggested Courses

801 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