int main() { int x; return 0; } ...">

Why am I getting the "Uninflated Local Variable" warning? (C ++)

When I do something like

#include<iostream>
int main()
{
    int x;
    return 0;
}

      

I'm getting a warning that x is an unaccepted local variable (I suppose because I created a variable and then didn't use it), why is this giving me a warning though?

+1


a source to share


6 answers


Probably because you are not wasting anything.



Plus, the code gets messy and hard to understand, not to mention programmers don't usually define variables they don't need, so it's kind of "is this really what you meant?" warning.

+19


a source


Because usually people don't create unintended variables on purpose. Therefore, if there is an undescribed variable in the program, it is usually a sign that you have an error somewhere, and the compiler will warn you about it.



+27


a source


Probably to stop something like this:

void some_func() {
    int a, b, c, d, e;
    ...
    do_something_with(a);
    do_something_with(b);
    do_something_with(c);
    do_something_with(d);
    do_something_with(c); // after hours of reading code, e looks like c: BUG!!
}

      

+15


a source


As an aside, I secretly dump unused variables as a quick dirty TODO mechanism when developing code ... flame away:

bool doSomething(...)
{
    int dontForgetToReplaceStubWithSomethingReal;
    return false;
}

      

+2


a source


it also lets you know what if you think you are using a variable and you don't know. assuming you created the variable for some reason, and maybe you forgot to use it somewhere.

0


a source


Or maybe they expected its constructor to have a side effect when used in scope, and its destructor a different side effect when extending it, and doesn't want the compiler to be as "useful" as people know best. when it comes with the intention of another code.

0


a source







All Articles