Solved

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

Posted on 2012-03-30
6
385 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to convert MFC APP to Win32 APP. 19 80
Specific format 21 198
Python 2.7 - French characters 6 81
groupNoAdj 7 96
This article will show, step by step, how to integrate R code into a R Sweave document
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

777 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