Questions tagged [gcc-warning]

GCC is the GNU Compiler Collection, encompassing the gcc C compiler and the g++ C++ compiler, among others. It has powerful warning facilities that can reveal coding errors in C and C++ code.

GCC is the GNU Compiler Collection, a collection of language compilers that use the same underlying infrastructure. The most prominent are gcc, for C, and g++, for C++. All GCC compilers have similar warning facilities, starting with -Wall and including many other -W flags. These warnings can be extremely helpful in diagnosing problems with code and improving coding style and portability.

824 questions
48
votes
3 answers

How to use C++20's likely/unlikely attribute in if-else statement

This question is about C++20's [[likely]]/[[unlikely]] feature, not compiler-defined macros. This documents (cppreference) only gave an example on applying them to a switch-case statement. This switch-case example compiles perfectly with my compiler…
Leedehai
  • 3,660
  • 3
  • 21
  • 44
45
votes
4 answers

Why uninitialized instead of out-of-bounds?

In the code below why is b[9] uninitialized instead of out-of-bounds? #include int main(void) { char b[] = {'N', 'i', 'c', 'e', ' ', 'y', 'o', 'u', '!'}; printf("b[9] = %d\n", b[9]); return 0; } Compiler call: % gcc -O2 -W…
Goswin von Brederlow
  • 11,875
  • 2
  • 24
  • 42
40
votes
5 answers

How can I make this GCC warning an error?

I get this warning from GCC: warning: cannot pass objects of non-POD type 'class Something' through '...'; call will abort at runtime It's pretty deadly, especially since it calls an abort. Why isn't this an error? I would like to make it an…
user34537
35
votes
1 answer

Why does gcc have a warning for long long?

What is the reason for the -Wlong-long gcc warning? From the gcc man page: -Wlong-long Warn if long long type is used. This is enabled by either -Wpedantic or -Wtraditional in ISO C90 and C++98 modes. To inhibit the warning messages, use…
Patrick
  • 2,243
  • 2
  • 23
  • 32
35
votes
4 answers

Warning: cast to/from pointer from/to integer of different size

I'm learning Pthreads. My code executes the way I want it to, I'm able to use it. But it gives me a warning on compilation. I compile using: gcc test.c -o test -pthread with GCC 4.8.1. And I get the warning test.c: In function…
user159
  • 1,343
  • 2
  • 12
  • 20
33
votes
2 answers

std::unique_ptr of base class holding reference of derived class does not show warning in gcc compiler while naked pointer shows it. Why?

I have a hierarchy of base class and derived class. Base class has one virtual function which is overridden by derived class. class Base { public: ~Base(); virtual void other_functionality() = 0; }; class Derived : public…
gaurav bharadwaj
  • 1,669
  • 1
  • 12
  • 29
32
votes
8 answers

GCC missing braces around initializer

I have this struct in C below that I want to initialize to all zero. How do I get rid of the missing braces warning? typedef struct { uint32_t incoming[FRAME_TYPE_MAX]; uint32_t outgoing[FRAME_TYPE_MAX]; uint32_t timeouts; uint32_t…
fred basset
  • 9,774
  • 28
  • 88
  • 138
31
votes
3 answers

How to do an explicit fall-through in C

The newer versions of gcc offer the Wimplicit-fallthrough, which is great to have for most switch statements. However, I have one switch statement where I want to allow fall throughs from all case-statements. Is there a way to do an explicit fall…
dlasalle
  • 1,615
  • 3
  • 19
  • 25
30
votes
3 answers

Why does gcc -Wall give warning about zero-length format string?

I searched around a little bit for information on this but didn't find anything satisfactory. Is there some special behavior to the function call sprintf(someString, ""); that explains why this is warning (on gcc with -Wall)? I only managed to…
SirGuy
  • 10,660
  • 2
  • 36
  • 66
30
votes
5 answers

gcc warning: braces around scalar initializer

I have look-up-table as defined below and I'm making use of GCC. When I compile I get warnings as warning: braces around scalar initializer What does this warning mean? How should I initialize this LUT? Am I making a mistake in initializing this…
HaggarTheHorrible
  • 7,083
  • 20
  • 70
  • 81
30
votes
4 answers

How to eliminate external lib/third party warnings in GCC

In the software project I'm working on, we use certain 3rd party libraries which, sadly, produce annoying gcc warnings. We are striving to clean all code of warnings, and want to enable the treat-warnings-as-errors (-Werror) flag in GCC. Is there a…
Michael
  • 2,826
  • 4
  • 25
  • 18
30
votes
8 answers

What's a proper way of type-punning a float to an int and vice-versa?

The code below performs a fast inverse square root operation by some bit hacks. The algorithm was probably developed by Silicon Graphics in early 1990's and it's appeared in Quake 3 too. more info However I get the following warning from GCC C++…
plasmacel
  • 8,183
  • 7
  • 53
  • 101
29
votes
2 answers

Avoid or warn on implicit conversion from const char* to bool in GCC

Consider the following code: void foo(bool parameter) { std::cout << parameter << "\n"; } int main() { foo("const char *argument"); } I want the compiler to raise a warning when passing const char* instead of bool as a parameter to…
29
votes
3 answers

What does if((x=0)) mean in C?

So apparently, in gcc/C, a compiler compiles when if ((x=0)){ some code } is used, while when if (x=0){ some code } is used, then compiler refuses to compile. What are the differences between two? As a note, I know what is the difference between…
user56220
  • 327
  • 1
  • 3
  • 6
28
votes
5 answers

MSVC equivalent of __attribute__ ((warn_unused_result))?

I'm finding __attribute__ ((warn_unused_result)) to be very useful as a means of encouraging developers not to ignore error codes returned by functions, but I need this to work with MSVC as well as gcc and gcc-compatible compilers such as ICC. Do…
Paul R
  • 208,748
  • 37
  • 389
  • 560
1
2
3
54 55