W8004 'Object' is assigned a value that is never used

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Airslash
    New Member
    • Nov 2007
    • 221

    W8004 'Object' is assigned a value that is never used

    Hello,

    I have a function that returns a boolean to signal success or failure of the function and takes a long as argument, and a pointer to return the value.

    The idea is to run the function, pass the the long as argument and a pointer for retrieving an object from the heap.

    true = pointer has address to object
    false = pointer is not usable.

    this is the code:
    Code:
    bool BigBrother::IO::DriveManager::FindDriveToUse(long RecordId, Drive *Object) const
    {
    	// Check if we have been properly initialized.
    	assert(initialized == true);
    
    	// Create a bool value to store our return value. We assume false
    	// to begin with.
    	bool retvalue = false;
    
    	// Loop through the list of drives and compare the RecordID against
    	// the JumpPoint of that drive. Also check if the drive is usable.
    	for(unsigned int i = 0; i < drives.size(); ++i)
    	{
    		// Check if the RecordID is smaller then upcoming jump point.
    		// And check if the drive is usable.
    		if((RecordId < CalculateNextJumpPoint(i)) && (drives[i]->GetUsable() == true))
    		{
    			// The drive that matches these conditions is found. Take it's
    			// address and assign it to our out pointer.
    			Object = drives.at(i);
    
    			// Set the return value to true.
    			retvalue = true;
    
    			// break the for loop
    			break;
            }
    	}
    
    	// return the retval variable
    	return retvalue;
    }
    But now the compiler cries about w8004 : 'Object' is assigned a value that is never used.

    I don't like warnings , so i'm kinda curious on how to solve this properly...
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    You are right not to like warnings because that particular warning is actually telling you that your whole function is not working as you intend it to.

    The warning is correct, to assign a value to a local parameter Object and you never then never use that value. You have made an unnecessary local assignment that has no effect, the value is not used locally and it is not passed out of the function.

    Remember you are using pass by value, in you passed an int into a function you would not expect to be able to alter the value of the int externally to the function. If you pass a pointer into a function you can not alter the value of the pointer outside the function, you can alter the value of the thing pointed to. You want to alter the value of a pointer outside the function so you need to pass a pointer to a pointer into the function, or since this is C++ a reference to a pointer.

    However there is a simpler solution, return the pointer and if you have no pointer to return return NULL.

    Comment

    • Airslash
      New Member
      • Nov 2007
      • 221

      #3
      my senior engineer here at work beat you to it with the same explanation :P

      But thanks for it^^, I understand now what the reason is and got it solved by indeed passing a pointer to a pointer instead of just a pointer.

      I prefer this kind of programming because I now have a nice bool to check against instead of comparing a pointer to null.
      Just a personal preference really.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        One good way to design functions is to use the arguments for passing back results and the return for passing back success/fail codes.

        As a rule I would avoid using a return to be either data (returning a pointer) or an error (returning the same pointer). If you have 25 error conditions, you can't return what happened with a null/not null setup.

        For production work, I use a single enmum for every error possible in the universe and have my functions return an enum value.

        Microsoft does this with the COM HRESULT so that there are multiple success codes and multiple error codes.

        Comment

        Working...