[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2012-03-30
6
Medium Priority
?
395 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 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

Industry Leaders: 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!

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Suggested Courses
Course of the Month18 days, 6 hours left to enroll

830 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