tags:

views:

246

answers:

6

I have a method that is suppose to return a garbage value if an item in a tree is not found. All I'm getting though is a runtime exception: "garbage is being used without being defined"

ItemType BstClass::rRetrieve(node* trav, KeyType key, bool& inTree)
{
    if(trav == NULL)
    {
     inTree = false;
     ItemType garbage;
     return garbage - 1;
    }
    if(trav->data.key == key)
    { 
     inTree = true;
     return trav->data; 
    }
    else if (key < trav->data.key) 
     return(rRetrieve(trav->left, key, inTree)); 
    else
        return(rRetrieve(trav->right, key, inTree)); 
}// end rRetrieve

Is there another way to do this? We were told to use

return garbage - 1;
+4  A: 

Without more info this is only a guess but how about:

ItemType garbage = -1;
return garbage;
fupsduck
"blind guess" quality, but yeah there is too little information to make a good answer
Gregory Pakosz
+2  A: 

A garbage value is any value NOT in the expected set of valid values that a function can return but is in the set of values that the function MAY return. For instance, if your tree contains positive numbers and you return a signed int from your tree search function, then a garbage value is any negative number. In this case I would return -1 as the garbage value.

For your case, I would probably replace the line

return garbage - 1;

with

return garbage = (ItemType)NULL;
David Harris
+1  A: 

I know this is probably way out of scope, but you could use boost::optional when you have no sensibile values to return. Read more at: http://cplusplus.co.il/2009/12/04/boost-optional-and-its-internals/

rmn
A: 

It looks like you're using 'garbage' in a computation without having previously initialized it. Perhaps

ItemType garbage = 0;
return garbage - 1;

Share and enjoy.

Bob Jarvis
+1  A: 

The main property of "garbage" is that it can be anything. There's no way to say whether some value is garbage or not by just looking at the value. So, initialize your garbage variable with some arbitrarily chosen but definitive value (zero, for example) and return it.

In fact, in C++ language any attempts to manipulate the value of an uninitialized variable in general case result in undefined behavior. You simply can't return genuine garbage in C++. You have to initialize your variable if you want to return it or to apply arithmetic operations to it.

AndreyT
+1  A: 

The answer depends on details of class ItemType which we don't have access to. Depending on what sort of operations you can do with ItemType and what sort of requirements you have about the 'garbage' value, you theoretically have a couple of choices.

  1. Directly construct and return your 'garbage' value. Something like return ItemType(-1); or return ItemType(NULL); Specifically how you construct a garbage value depends on what constructors you have and their semantics.
  2. Perhaps you shouldn't even care about the specific value. Since you have the inTree output variable indicating when you haven't found a value, then perhaps just return the default value return ItemType(); This is not as robust because the code is no longer redundant (i.e. for method 1, if the caller does not check inTree then hopefully using the 'garbage' will prevent the code from doing something wrong).
R Samuel Klatchko