473,406 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

Airslash
221 100+
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:
Expand|Select|Wrap|Line Numbers
  1. bool BigBrother::IO::DriveManager::FindDriveToUse(long RecordId, Drive *Object) const
  2. {
  3.     // Check if we have been properly initialized.
  4.     assert(initialized == true);
  5.  
  6.     // Create a bool value to store our return value. We assume false
  7.     // to begin with.
  8.     bool retvalue = false;
  9.  
  10.     // Loop through the list of drives and compare the RecordID against
  11.     // the JumpPoint of that drive. Also check if the drive is usable.
  12.     for(unsigned int i = 0; i < drives.size(); ++i)
  13.     {
  14.         // Check if the RecordID is smaller then upcoming jump point.
  15.         // And check if the drive is usable.
  16.         if((RecordId < CalculateNextJumpPoint(i)) && (drives[i]->GetUsable() == true))
  17.         {
  18.             // The drive that matches these conditions is found. Take it's
  19.             // address and assign it to our out pointer.
  20.             Object = drives.at(i);
  21.  
  22.             // Set the return value to true.
  23.             retvalue = true;
  24.  
  25.             // break the for loop
  26.             break;
  27.         }
  28.     }
  29.  
  30.     // return the retval variable
  31.     return retvalue;
  32. }
  33.  
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...
Feb 5 '10 #1
3 6321
Banfa
9,065 Expert Mod 8TB
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.
Feb 5 '10 #2
Airslash
221 100+
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.
Feb 5 '10 #3
weaknessforcats
9,208 Expert Mod 8TB
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.
Feb 6 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

106
by: A | last post by:
Hi, I have always been taught to use an inialization list for initialising data members of a class. I realize that initialsizing primitives and pointers use an inialization list is exactly the...
28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
4
by: Luke Matuszewski | last post by:
Here are some questions that i am interested about and wanted to here an explanation/discussion: 1. (general) Is the objectness in JavaScript was supported from the very first version of it (in...
26
by: yb | last post by:
Hi, Is there a standard for the global 'window' object in browsers? For example, it supports methods such as setInterval and clearInterval, and several others. I know that w3c standardized...
12
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. ...
12
by: satyajit | last post by:
I am trying to learn the concept of constructors in ECMAScript. I executed following code (See execution in Rhino JavaScript shell): function Foo(a) { this.a = a; } function Bar(b) { this.b...
3
by: User1014 | last post by:
A global variable is really just a property of the "Global Object", so what does that make a function defined in the global context? A method of the Global Object? ...
4
by: alex | last post by:
I am so confused with these three concept,who can explained it?thanks so much? e.g. var f= new Function("x", "y", "return x * y"); function f(x,y){ return x*y } var f=function(x,y){
2
by: Ralph | last post by:
Hi I don't understand why it's not working: function schedule(imTop){ this.tdImagesTop = imTop; } schedule.prototype.selectEl = function() { alert(this.tdImagesTop);
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.