Is the following C program guaranteed to exit with 0
or is the compiler allowed to identify the objects s
and t
with one another as is permitted in C++ as the so-called named return value optimization (NRVO) form of copy elision?
typedef struct {
int i, j;
double a, b;
} S;
int result;
S test(S *q) {
S s = {0, 0, 0, 0};
result = &s == q;
return s;
}
int main(void)
{
S t = test(&t);
return result;
}
Clang exits with 1
against my expectations, see https://godbolt.org/z/ME8sPGn3n.
(In C++, both 0
and 1
are valid outcomes due to explicit permission to perform NRVO.)
It is guaranteed to exit with 0. t
and s
are different objects, pointers to them can't compare equal.
About the code that you removed:
What about the following variation in which relevant lifetime considerations may be different?
From https://port70.net/~nsz/c/c11/n1570.html#6.2.4p2 :
The value of a pointer becomes indeterminate when the object it points to (or just past) reaches the end of its lifetime.
It may return 1 or 0 or perform a trap.