Thursday, 22 August 2013

Simplest way to determinize a C program

Simplest way to determinize a C program

Consider the C program below:
#include <stdio.h>
int f() {
printf("f");
return 1;
}
int g() {
printf("g");
return 2;
}
int main() {
return f() + g();
}
According to the C standard, this program does not have a single
deterministic behavior, due to the sum in the main function, which
consists in two subexpressions, and the following excerpt from the C99
standard:
§6.5 (...) the order of evaluation of subexpressions and the order in
which side effects take place are both unspecified.
Therefore, printing fg and gf are both valid outputs for this program. In
practice, a given compiler will choose a single fixed evaluation order
(e.g., left-to-right for gcc in this case), but if I want to reliably
compare the output among different compilers, I need to ensure my program
has a single defined behavior.
My question is: what is the simplest way to do it? Is there a way to avoid
including temporary variables (e.g., int tmp = f(); return tmp + g();)?

No comments:

Post a Comment