Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does gcc not give a warning at undefined behaviour in code inside?

Tags:

People also ask

How do I make GCC warnings as errors?

You can use the -Werror compiler flag to turn all or some warnings into errors. Show activity on this post. You can use -fdiagnostics-show-option to see the -W option that applies to a particular warning. Unfortunately, in this case there isn't any specific option that covers that warning.

What causes undefined Behaviour in C?

So, in C/C++ programming, undefined behavior means when the program fails to compile, or it may execute incorrectly, either crashes or generates incorrect results, or when it may fortuitously do exactly what the programmer intended.

What causes undefined behavior C++?

In C/C++ bitwise shifting a value by a number of bits which is either a negative number or is greater than or equal to the total number of bits in this value results in undefined behavior.

What is undefined behavior in programming?

In computer programming, undefined behaviour is defined as 'the result of compiling computer code which is not prescribed by the specs of the programming language in which it is written'.


I just read this SO C++ FAQ about undefined behavior and sequence points and experimented a bit. In the following code gcc-4.5.2 gives me a warning only in the line mentioned in the code comment, although the one line before shows undefined behaviour too, doesn't it? You can't say which operand of addition is executed first (as + is no sequence point). Why does gcc not give me a warning in this line too?

int i=0;
int j=0;

int foo(void) {
    i=1;
    return i;
}

int main(void) {
    i = i + foo(); 
    j = j + (j=1); //Here is a rightly warning
    return 0;
}

Thank you for helping.