All four cases cause undefined behavior in C, since an uninitialized automatic variable never has its address. See Another answer.
By the way, sizeof(x) is defined because the expression is not actually evaluated: it is an estimate of the compilation time, which decays into a type.
In the latest C ++ 1y project ( N3936 ), this is clearly undefined behavior, since the language of undefined values and undefined behavior have been refined , and now it says in section 8.5 :
[...] If an undefined value is created by an estimate, the behavior is undefined, except in the following cases:
and further lists the exception for only certain types of unsigned characters.
In C ++, we used to rely on underspecified lvalue-to-rvalue conversion to prove undefined behavior, which is problematic in the general case. In this case, we have a lalue-to-rvalue conversion. If we look at section 5.2.2 Calling function 7, which reads (emphasis mine):
If there is no parameter for this argument, the argument is passed in such a way that the receiving function can obtain the value of the argument by calling va_arg (18.10). [...] The value lvalue-to-rvalue (4.1) , the pointer variable (4.2) and the to-pointer function (4.3) standard transformations are performed with the argument expression .
Bathsheba Apr 03 '14 at 13:38 on 2014-04-03 13:38
source share