Away with those warnings!

Programming, for all ages and all languages.
Post Reply
mr. x

Away with those warnings!

Post by mr. x »

How do I remove the warning messages outputted from GCC? (Don't say fix them, because the warnings are like "return makes integer from pointer without a cast", casting everywhere uglyfies the code quite a bit).
Joel (not logged in)

Re:Away with those warnings!

Post by Joel (not logged in) »

Sorry, but fix them.

Yes, casts are ugly, but that's because they should be avoided wherever possible. When they are necessary, their ugliness makes them stand out as potential sources of errors.
Schol-R-LEA

Re:Away with those warnings!

Post by Schol-R-LEA »

If you don't mind me asking, under what circumstances are these casts being made, anyway? Are you referencing constant addresses, or performing some kind of computed addressing that can't work with ordinary pointer arithmetic? Just curious, as usually such casts are something better avoided when possible.
Nychold

Re:Away with those warnings!

Post by Nychold »

Warnings will not affect whether the program will or will not compile (unless you tell the compiler to stop after x warnings). The compiler is just saying "Hey, I'm not entirely sure I know what's going to happen here, so if something breaks, I warned you." Generally, in large complex programs, warnings are expected and in a lot of cases, programmers actually tell the compilers not to tell them about the warnings (like #pragma warning(disable: w1 w2 w3 ... ) where w1 ... are the warning numbers). However, this should ONLY be done if you know the warnings are ill-founded and are even worse in programming practice than verbose type conversions. XD
Tim

Re:Away with those warnings!

Post by Tim »

Nychold: IMO that's very bad advice. Warnings are the compiler saying, "You've done something pretty weird. I can carry on compiling, but you really need to work out what you've done wrong".

Some warnings can be safely silenced; for example, VC6 emits warnings when the decorated name of some templated class exceeds 255 characters. But some point to dangerous code. Yours sounds like one of those cases.

Any time you get a warning, you need to investigate why it's happening and what you can do to fix your code. Don't just shut the compiler up, maybe by experimenting with typecasts until it compiles; instead, understand what's happening. If necessary, rewrite the code so that the warning doesn't happen in the first place. It is possible, and desirable, to write code which doesn't give warnings. It's worth getting into the habit of enabling the compiler option which turns warnings into errors.
zxfhm

Re:Away with those warnings!

Post by zxfhm »

warning: return type of main() is not int
Nychold

Re:Away with those warnings!

Post by Nychold »

Maybe what I said came out wrong. I was not giving advice, merely stating facts, and coding principles which I've seen done in the past. By all means, look through the Win32 headers. #pragma warning(disable: blah) is everywhere. In fact, in Windows.h, I counted 6 of them.

Plus, I've downloaded dozens of programs, and compiled them with massive warnings (one, which will stay nameless, received well over 300 warnings), but still run fine. I'm definitely of the belief that all problems, no matter how small, should be addressed and fixed. But, I'd be lying if I said I have never turned a deaf ear to a few warnings here and there. XD
Post Reply