[LLVMdev] Uninitialized variable - question

john skaller skaller at users.sourceforge.net
Sat Nov 24 03:16:54 PST 2012


On 24/11/2012, at 9:08 PM, Jakub Staszak wrote:

> Hello,
> 
> I was wondering about the case below. I tried to find any information in C standard, but I found nothing.
> In this case, variable "i" is uninitialized, but it is the _same_ value passed as an argument, so only of "a" or "b" should be printed.
> 
> What I found is that with -O2:
> LLVM (trunk) prints both "a" and "b"
> GCC (4.2) prints both "a" and "b"
> GCC (trunk) prints "b" only.
> 
> As I said, I don't know what standard says here.

Depends which Standard you read, and which part of it :)

ISO C99, 6.2.6.1/5
	Certain object representations need not represent a value of the object type. 
If the stored value of an object has such a representation and is read by an lvalue 
expression that does not have character type, the behavior is undefined.

ISO C99 says, in 6.5/5 "If an exceptional condition occurs during
the evaluation of an expression (THAT IS, IF THE RESULT IS NOT 
MATHEMATICALLY DEFINED  ... ) the behaviour is undefined.
[Emphasis mine]

This suggests that the use of an unspecified value leads to undefined
behaviour in which case all the results you got are permitted.
Unfortunately the C Standard is not known for good wording or
consistency.

C99 attempted to over-constrain integer type representations (IMHO),
and the specifications are themselves not well defined and therefore not
normative. (This is what happens when the political driving forces
don't know any mathematics and barely understand an CS).

I haven't seen the C++11 wording on this. Hopefully they didn't
follow ISO C here.

> 
> #include <stdio.h>
> 
> void f(int i) __attribute__((noinline));
> void g(int i) __attribute__((noinline));
> 
> void f(int i) {
>  if (i) printf("a\n");
> }
> 
> void g(int i) {
>  if (!i) printf("b\n");
> }
> 
> int main() {
>  int i;
>  f(i);
>  g(i);
> }
> 
> 
> - Kuba
> 
> 
> 
> 
> 
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

--
john skaller
skaller at users.sourceforge.net
http://felix-lang.org







More information about the llvm-dev mailing list