tags:

views:

1207

answers:

9

I have an array of doubles and need to do a calculation on that array and then find the min and max value that results from that calculation. Here is basically what I have:

double * array;
double result;
double myMin;
double myMax;

// Assume array is initialized properly...
for (int i = 0; i < sizeOfArray; ++i) {
    result = transmogrify(array[i]);
    if (i == 0) {
        myMin = result;
        myMax = result;
    }
    else if (result < myMin) {
        myMin = result;
    }
    else if (result > myMax) {
        myMax = result;
    }
}

I'm getting a warning that the value computed for result is never used, and since we treat all warnings as errors, this doesn't compile. How can I fix this code to avoid the warning? I'm using g++ for my compiler.

Here's the warning text:

cc1plus: warnings being treated as errors
foo.cc:<lineno of transmogrify call>: error: value computed is not used

Edit: I don't understand the down votes, but I've got things working now. Thanks to everyone for taking the time to help me out.

+3  A: 

Assuming you don't need result outside of the loop, you could declare result inside the loop thusly:

for( int i=0; i < sizeOfArray; ++i ) {
    double result = transmogrify( array[i] );
    ...
}
Graeme Perrow
It would not solve the problem, because the code as posted by the user doesn't have a prioblem.
anon
Turns out this does solve the problem. Unfortunately I can't paste my actual code.
Scottie T
If you can't paste the actual code, at least paste something that shows the problem you want solved. Not doing so wastes everybody's time and shows disrespect for the community.
David Thornley
if it finds out that myMin and myMax are not used, it can transitively deduce that result isn't used either. Maybe that's the problem?
Johannes Schaub - litb
David, what else could I do? I took the time to write up as clear of a question as I could on this warning. By no means do I want to show disrespect to the community, and I definitely appreciate the time everyone spent trying to figure this out on my behalf.
Scottie T
@Scottie - you could have compiled the code you posted, which would have told you that it did not exhibit the error you claimed it did.
anon
Scottie T, well for one, it doesn't say in the warning message that "result" is not used. it just says "value computed not used". it may aswell mean array[i]. For another, I would post the warning text from the start on, it would be the first i would do. Then, someone asked you for the compiler version and options used. you kept saying they are all "secret". how do you think we should help you then effectively. Nevertheless, i think i will save my downvotes for other worse questions. cheers
Johannes Schaub - litb
@Neil, true, I should have done that, and perhaps posted the code anyway with a note saying this snippet does not reproduce the warning I'm getting on my machine.
Scottie T
+1  A: 

I don't think the code as posted should produce the error, unless the compiler is doing some phenomenal flow analysis. It certainly compiles OK with g++, but I'm not sure that g++ even supports the warning you are getting.

The following adaptation of your code, which preserves its structure, produces no error with g++:

int main() {
double * array;
double result;
double myMin;
double myMax;

double t(double);

// Assume array is initialized properly...
for (int i = 0; i < 10; ++i) {
    result = t(array[i]);
    if (i == 0) {
        myMin = result;
        myMax = result;
    }
    else if (result < myMin) {
        myMin = result;
    }
    else if (result > myMax) {
        myMax = result;
    }
}

}

anon
maybe it warns because sizeOfArray *could* be zero. It doesn't know at compile time yet, so it thinks "hmm, better a false positive than not warning at all". So because if the for loop would not be taken, result is never used anywhere
Johannes Schaub - litb
Replacing 10 with a function call (which could return zero) makes no difference.
anon
A: 

Removing the first else only impacts the running time of the first run through the loop. I'd eliminate it rather.

if (i == 0) {
    myMin = result;
    myMax = result;
}
if (result < myMin) 
    myMin = result;
else if (result > myMax) 
    myMax = result;
Pontus Gagge
+1  A: 
result = [...]
if (i == 0) {
    [... do something with result ...]
}
else if (result < myMin) {

In both branches of the if(), result is used. In the 1st case it's assigned to a variable, in the 2nd it's used in a comparison. So the compiler shouldn't warn.

I suspect you might have misdiagnosed the problem. Please can you say exactly what the error message is (copy-paste it). Also, please try to post the smallest piece of code you can that actually compiles and gives the warning? (Simply trying to do that will probably let you find the problem)

EDIT: Is it possible that transmogrify() is a macro that uses result internally?

user9876
transmogrify is a function, not a macro.
Scottie T
+2  A: 

Initialize myMin and myMax with DBL_MAX and DBL_MIN respectively and get rid of the first time through the loop check.

Trent
+4  A: 

I'm getting a warning that the value computed for result is never used because (theoretically) it's possible that none of the if/else branches will be selected

That can't be the reason for the warning, because result is also used in the if conditions. Even if none of the branches are taken, result is still used to decide that they should not be taken.

sth
What I came to post too. Pasted code is incorrect or compiler error is faulty.
Pool
Nope, even if "i" is always zero, result will be used.
Pool
Oh yeah, you're right of course.
sth
result is always used. the question, however, is whether it's used outside the loop i think. If not, he should try Graeme's stuff i think.
Johannes Schaub - litb
The error said "value computed for result" not "value might never be used / initialized" - I think the compiler error is incorrect.
Pool
+1  A: 

Before the "if" statement:

result = 0.0;

or some other value. It is always good form to set a variable to some value before using it.

jay
+1  A: 

I'm getting a warning that the value computed for result is never used because (theoretically) it's possible that none of the if/else branches will be selected, and since we treat all warnings as errors, this doesn't compile. How can I fix this code to avoid the warning? I'm using g++ for my compiler

The value used for result is always used. If not assigned it's used in the comparator. Therefore the compiler is faulty.

Pool
A: 

A quick solution might be to unroll the first iteration, like this:

double * array;
double result = transmogrify(array[0]);
double myMin = result;
double myMax = result;
int i;

for (i = 1; i < sizeOfArray; ++i) {
    result = transmogrify(array[i]);

    if (result < myMin) {
        myMin = result;
    }

    if (result > myMax) {
        myMax = result;
    }
}

EDIT: I'll expand a bit on this. You haven't given any detailed information on sizeOfArray, but my guess is that it's a signed integer type.

I believe you've misunderstood the cause of the warning, result might be unused because sizeOfArray might be less than or equal to zero, not because of the if..else clauses inside the loop. In the code above, it might be a bit clearer why you need careful handling of the case (sizeOfArray <= 0), but it's equally important in the original code snippet as well.

Christoffer
Did you try compiling this before you posted it? It has no effect.
anon
No, I'm just writing from the top of my head. Just edited the answer though, I saw that the suggestion using DBL_MIN and DBL_MAX had already been given. It's possible that this fares better.
Christoffer
...and of course this suggestion calls for different input sanitation than the original posters'
Christoffer