Hi
Increase warning level on compiler so warnings is good or not?
Or in which cases is it good to use?
Hi
Increase warning level on compiler so warnings is good or not?
Or in which cases is it good to use?
you tell me you can C,why dont you C your own bugs?
Generally you should use the highest warning level your compiler supplies. The more warnings that are caught means the fewer potential problems you will have later.
I agree with jimblumberg if your code is intended to go out into the wild (such as a gitlab hosted project) but if it's only on your computer then -Wall or /Wall (depends on compiler) is enough to cover your basis, I don't remember any more warning options for MSC but for GCC and ones based on it I believe there was the -Wextra option which is helpful for ironing out potential problems that are normally ignorable on the average set up, things like unused symbols for example.
In the past I have been using "-Wall -pedantic", but now also recently added "-Wextra", which for me picks up the mixing of mostly signed vs unsigned issues.
It has shown me a lot of shabby/fuzzy thinking I had, usually around just using "int" as the loop variable for "for" loops, where "size_t" might be a better choice.
With gcc/g++ I would also suggest that you specifically state the version of the standard you want to use, ie "-std=c++20" because by default gcc/g++ uses "-std=gnu++XX". I recommend sticking with one of the standards instead of the gnu hacks unless you actually need them.
One other warning I normally use is "-Wvla", especially in C++ since IMO vla are a horrible addition to the C standard and are disallowed in C++.
I strongly disagree. Warnings are there for a reason! If you ignore a warning now, when your code is small, it might come back to bite you later as code is added, or worse, causes an intermittent error difficult to track down.
I strongly agree with jimblumberg, and recommend setting your warning level as high as your compiler allows, always, and don't ignore the warnings shown. This recommendation is especially true for any beginner C programmers reading this thread, and others.
For beginner C programmers that's fine but for experienced C programmers who are only trying out something Wall is enough, any project that's only meant for one's own computer is usually gonna stay small, and more importantly they're usually gonna be a bodge it job, where it's NOT expected to grow and/or possibly go into the wild, it also serves as good practice for theory tests where you don't have the luxury of a compilers, as a side bonus it helps one become more comfortable calling themself a minor expert (I say minor because despite my experience I don't feel comfortable calling myself a major expert until I start doing kernel level programming)
That's idiotic. You should crank it up at least as high as -Wall -Wextra -Wpedantic. In fact you can add more, such as -Wshadow to detect shadowing a variable, a common beginner mistake (although pros might find it more annoying than useful).Wall is enough [for a beginner]
Ordinary language is totally unsuited for expressing what physics really asserts.
Only mathematics can say as little as the physicist means to say. - Bertrand Russell
From time to time I like to use "-Wmissing-include-dirs" to verify my program is not including folders that do not exist.
Edit: This is a problem that is normally IDE or makefile caused.
Tim S.
Last edited by stahta01; 07-29-2021 at 12:30 PM.
"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson
I disagree, IMO, experienced programmers prefer to have the compiler help "debug" the program. I usually use quite a few warnings when writing code, even though I will rarely have code that generates some of those warnings (ie: -Wmain). By the way, IMO, warnings are errors in disguise, so never ignore the warnings, even some of the easily understood/explained warnings (-Wmissing-declarations).For beginner C programmers that's fine but for experienced C programmers who are only trying out something Wall is enough,
That's good because looking at some of your code I would say that you're an intermediate programmer at best.say minor because despite my experience I don't feel comfortable calling myself a major expert
I hope you realize that there are quite a few experts around that have never done much if any kernel level programming, and just because someone is playing around with kernel source doesn't necessarily make them an expert at anything.I don't feel comfortable calling myself a major expert until I start doing kernel level programming
I downloaded your "deflate.zip" you publicly posted.
gcc version: 10.2.1 on Debian Linux Testing
No warnings or errors displayed.Code:gcc -Wall main.c
Need I say more?Code:gcc -std=c18 -Wall -Wextra -Wpedantic main.c: In function ‘PrintStreamDetails’: main.c:222:15: warning: format ‘%p’ expects argument of type ‘void *’, but argument 5 has type ‘STREAM *’ {aka ‘struct _STREAM *’} [-Wformat=] 222 | "%s:%u %s( %p ): ptr = %p, pos = %u, num = %u, max = %u, fed = %u, got = ", | ~^ | | | void * ...... 226 | stream, | ~~~~~~ | | | STREAM * {aka struct _STREAM *} main.c: In function ‘PrintSymbol’: main.c:295:50: warning: operand of ‘?:’ changes signedness from ‘int’ to ‘uint’ {aka ‘unsigned int’} due to unsignedness of other operand [-Wsign-compare] 295 | uint sym = isprint(symbol->sym) ? symbol->sym : -1; | ^~ main.c:296:50: warning: operand of ‘?:’ changes signedness from ‘int’ to ‘uint’ {aka ‘unsigned int’} due to unsignedness of other operand [-Wsign-compare] 296 | uint lit = isprint(symbol->lit) ? symbol->lit : -1; | ^~ main.c:300:26: warning: format ‘%p’ expects argument of type ‘void *’, but argument 5 has type ‘HUFFMAN_SYMBOL *’ {aka ‘struct _HUFFMAN_SYMBOL *’} [-Wformat=] 300 | "%s:%u: symbols[%3u] (%p): " | ~^ | | | void * ...... 309 | symbol, | ~~~~~~ | | | HUFFMAN_SYMBOL * {aka struct _HUFFMAN_SYMBOL *} main.c: In function ‘IdentifyNextSymbol’: main.c:386:16: warning: format ‘%p’ expects argument of type ‘void *’, but argument 5 has type ‘STREAM *’ {aka ‘struct _STREAM *’} [-Wformat=] 386 | "%s:%u: %s( %p, %p, %u, %s ) Failed to identify next symbol\n" | ~^ | | | void * ...... 390 | , stream | ~~~~~~ | | | STREAM * {aka struct _STREAM *} main.c:386:20: warning: format ‘%p’ expects argument of type ‘void *’, but argument 6 has type ‘HUFFMAN_SYMBOLS *’ {aka ‘struct _HUFFMAN_SYMBOLS *’} [-Wformat=] 386 | "%s:%u: %s( %p, %p, %u, %s ) Failed to identify next symbol\n" | ~^ | | | void * ...... 391 | , Symbols | ~~~~~~~ | | | HUFFMAN_SYMBOLS * {aka struct _HUFFMAN_SYMBOLS *} main.c: In function ‘main’: main.c:533:15: warning: unused parameter ‘argc’ [-Wunused-parameter] 533 | int main( int argc, char *argv[] ) | ~~~~^~~~ main.c: In function ‘RdHuffmanLengDistCodes’: main.c:867:20: warning: format ‘%p’ expects argument of type ‘void *’, but argument 5 has type ‘STREAM *’ {aka ‘struct _STREAM *’} [-Wformat=] 867 | #define PFX_FORMAT "%s:%u: %s( %p, %p, %p, %u )" | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~ main.c:877:10: note: in expansion of macro ‘PFX_FORMAT’ 877 | printf( PFX_FORMAT "\n", PFX_VALUES ); | ^~~~~~~~~~ main.c:867:33: note: format string is defined here 867 | #define PFX_FORMAT "%s:%u: %s( %p, %p, %p, %u )" | ~^ | | | void * main.c:867:20: warning: format ‘%p’ expects argument of type ‘void *’, but argument 6 has type ‘HUFFMAN_SYMBOLS *’ {aka ‘struct _HUFFMAN_SYMBOLS *’} [-Wformat=] 867 | #define PFX_FORMAT "%s:%u: %s( %p, %p, %p, %u )" | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~ main.c:877:10: note: in expansion of macro ‘PFX_FORMAT’ 877 | printf( PFX_FORMAT "\n", PFX_VALUES ); | ^~~~~~~~~~ main.c:867:37: note: format string is defined here 867 | #define PFX_FORMAT "%s:%u: %s( %p, %p, %p, %u )" | ~^ | | | void * main.c:867:20: warning: format ‘%p’ expects argument of type ‘void *’, but argument 7 has type ‘HUFFMAN_SYMBOLS *’ {aka ‘struct _HUFFMAN_SYMBOLS *’} [-Wformat=] 867 | #define PFX_FORMAT "%s:%u: %s( %p, %p, %p, %u )" | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~ main.c:877:10: note: in expansion of macro ‘PFX_FORMAT’ 877 | printf( PFX_FORMAT "\n", PFX_VALUES ); | ^~~~~~~~~~ main.c:867:41: note: format string is defined here 867 | #define PFX_FORMAT "%s:%u: %s( %p, %p, %p, %u )" | ~^ | | | void * main.c:864:14: warning: unused parameter ‘name’ [-Wunused-parameter] 864 | char const *name | ~~~~~~~~~~~~^~~~
No comment.As a side bonus it helps one become more comfortable calling themself a minor expert (I say minor because despite my experience I don't feel comfortable calling myself a major expert until I start doing kernel level programming)
The warnings about casting to void* in printf statements are not ignorable. Passing in the wrong pointer type (i.e., not a void pointer) has undefined behavior. Some architectures even have different sizes of pointers depending on the pointed-to type. The safest thing to do is cast the pointers to void*.
Thanks for answers all
Different pointer size must be more common,when you can compile c to 4 different cpu's
Using ebx gives me warning, so i preserve that
Seem easy to cause exception,writing near local variables
you tell me you can C,why dont you C your own bugs?
Back in the old days (of 16-bit IBM PCs) you had "near pointers" "far pointers" and "huge pointers".
'near' pointers were 16 bits, and could address up to 64k, but only in one block of memory - usually in the data or code segments.
'far' pointers were 32-bits, but could address 64k anywhere in 1MB of memory
'huge' pointers were 32-bits, very inefficient, but could address anywhere in 1MB of memory.
C compilers would compile almost anything without warnings, as they had to run on CPUs that might be 4.77MHz, and fit into only a few hundred kb of RAM.
Programmers learnt fast or died young back then. Ah, the good old days...
Explain Near Far Huge pointers in C language