c - If a variable is uninitialised, when does it throw as error and when does it gives a garbage value? -
c - If a variable is uninitialised, when does it throw as error and when does it gives a garbage value? -
i ran next codes using ideone.com:
#include <stdio.h> int main(void) { int i,j=0; if(j) { j=0; //to suppress warning j not used i=1; } printf("%d\n",i); } output: garbage value:
#include <stdio.h> int main(void) { int i; if(0) { i=1; } printf("%d\n",i); } output:
error: unitialized local variable used.
is because compiler removes if(0) block while optimizing? , such optimization cannot done in case of if(j) since variable? won't value of j nowadays @ compile-time , same optimization must done? or memory allocated @ run-time only?
as far can tell ideone using gcc, code using must using -werror since -wuninitialized warning if used in conjunction -werror turn error. can see when warning triggered dependent on optimization settings , version of gcc, documents say:
because these warnings depend on optimization, exact variables or elements there warnings depends on precise optimization options , version of gcc used.
note there may no warning variable used compute value never used, because such computations may deleted info flow analysis before warnings printed.
of online compilers available prefer coliru when want experiment compiler flags , different compilers since allows straight forwards way manipulate them.
at end of day using uninitialized variable undefined behavior , compiler not obliged generate warning this, compiler can unexpected things undefined behavior quesiton why code output more 4 lines? demonstrates.
c warnings undefined-behavior
Comments
Post a Comment