tags:

views:

157

answers:

5

If i declare a variable but not use it later in the program, the complier will give me a warning, and since "every warning should not be ignored", why the warning is there? and how can it cause a error? thanks!

+1  A: 

It may indicate the presence of a bug, a variable that is declared but unused is obviously a programming error, since why else would it be there at all?

1800 INFORMATION
+2  A: 

It's there because maybe you meant to use the variable. You generally don't declare a variable and then use not it :)

It's a helpful warning, and it exists in most languages.

It may assist in detecting typos, where you accidentally used another variable instead of the one you meant to, this warning will remind you of the one that you haven't used :)

Noon Silk
I think you mean "you generally don't declare a variable and then *not* use it" ;)
Shaggy Frog
Uh yes, thanks, fixed :)
Noon Silk
+2  A: 

It doesn't cause an error. It causes a warning (normally only at higher levels on most compilers) because the variable isn't doing anything, which might indicate that you intended to use it but didn't. Sometimes that might indicate that your code is behaving incorrectly.

It's not that the code could fail as it is, just that the compiler's trying to warn you that you've done something a little odd.

Peter
It does not cause a compiler error (unless you have specified an option to treat all warnings as errors) but I think the OP was wondering why it can be considered a *programming error* and therefore the compiler emits a warning for such variables.
Sinan Ünür
Yes, I was trying to explain that as well - but I didn't find the original question particularly clear. So I thought I'd aim for both :)
Peter
yes, I was thinking that all the warning would end up being a error in some cases, but to this one, I just didn't know how could it be. Now I'm clear about it, thank you all guys!
zjsworm
+4  A: 

First, a minor point: declaring a variable that's never used is a waste of memory, and thus is itself a bug.

Second, and more importantly: you took the trouble of writing out a declaration for a variable you then never used. Since you would not have bothered to declare a variable if you had no plan to use it, this suggests you've forgotten to use it! Is it possible you typed the wrong variable name in its place? Is it possible you forgot to perform a critical calculation whose result you'd store in that variable?

Of course, you might just have declared something you ended up not needing, which is why it's a warning and not an error, but it's easy to see situations where that warning can point you to an important piece of missing code, which would indeed be a bug.

VoteyDisciple
First point probably doesn't matter, because most compilers optimize it away, unless it's an object with a ctor and dctor which can't be optimized away.
strager
And if the constructor and destructor aren't optimized away, then it's not correct to say that the object isn't used (and if the compiler gives a warning, then it's a bug in the compiler).
Rob Kennedy
@Kennedy, I disagree. The variable isn't being used by the programmer, but an object's still being instantiated for it. The warning is about an object being instantiated without the object being used, not about a variable which does nothing.
strager
A: 

Are you asking about something like this?

int function() {
    double x;

    return 42;
}

There's a warning saying the variable x is unused because, well, it's unused. It is a warning because if a variable is unused, it does nothing*.

You may fall into this warning if you mistype or accidentally shadow a variable. For instance:

void printArgument10Times(int i) {
    for(int i = 0; i < 10; ++i) {
        std::cout << i << std::endl;
    }
}

// ...

printArgument10Times(42);

Instead of printing 42 ten times, it prints 0 through 9. i was shadowed and was also unused. The compiler will hopefully tell you both these things.

*In C++ constructors and deconstructors are called, of course.

strager