how to check that behavior is undefined in c? -
i know following undefined because trying read , write value of variable in same expression, is
int a=5; a=a++;
but if why following code snippet not undefined
int a=5; a=a+1;
as here trying modify value of a
, write @ same time.
also explain why standard not curing or removing undefined behavior, in spite of fact know undefined?
long story short, can find every defined behavior in standard. not mentioned there defined - undefined.
intuitive explanation example:
a=a++;
you want modify variable a
2 times in single statement.
1) a= //first time 2) a++ //second time
if here:
a=a+1;
you modify variable once:
a= // (a+1) - doesn't change value of
why don't standard define a=a++
behavior?
one of possible reasons is: compiler can perform optimizations. more cases define in standard, less freedom compiler has optimize code. because different architectures can have different increasing instructions implementations, compiler wouldn't use processor instructions in case break standard behavior. or in cases compiler can change evaluation order, restriction force compiler disable such optimizations if want modify twice.
Comments
Post a Comment