In C, does initialising a variable to it’s own value make sense? If yes, what for?
Allow me to elaborate. In Git sources there are some examples of initialising a variable to it’s own undefined value, as seen in transport.c or wt-status.c. I removed assignments from those declarations and run tests. Seeing no regressions, I thought that those assignments were redundant.
On the other hand, I did some simple tests with GCC 4.6 and Clang 2.9.
#include <stdio.h>
int main() {
printf("print to increase probability of registers being non-zero\n");
int status = status;
return printf("%i\n", status);
}
Compiling with -Wall -std=c99 and various -O levels prints no warnings and shows that status == 0. Clang with a non-zero optimisation level prints some garbage values though. It makes me infer that results of such expressions are undefined.
I can imagine that such assignment can suppress an uninitialised variable warning, but it’s not the case for the examples taken from Git. Removing assignments doesn’t introduce any warnings.
Are such assignments an undefined behaviour? If not, what do you use them for?
I’ve suggested the change on the Git mailing list. Here’s what I’ve learned.
This compiles because Standard C99 §6.2.1/7 says:
However, value of
statusis Indeterminate. And you cannot rely on it being initialized to something meaningful.How does it work?
int statuscreates an space for the variable to exist on the stack(local storage) which is then further read to performstatus = status,statusmight get initialized to any value that was present in the stack frame.How can you guard against such self Initialization?
gcc provides a specific setting to detect self Initializations and report them as errors:
Why is it used in this code?
The only reason I can think it is being used in the said code is to suppress the unused variable warning for ex: In
transport.c, if the control never goes inside thewhileloop then in that control flowcmpwill be unused and the compiler must be generating a warning for it. Same scenario seems to be withstatusvariable inwt-status.c