[LLVMdev] Uninitialized variable - question

Duncan Sands baldrick at free.fr
Sat Nov 24 03:11:03 PST 2012


Hi Jakub,

> 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.

I'm no C expert, but my understanding is that making any use of an uninitialized
variable results in totally undefined behaviour, where "totally" means anything
goes (eg erasing the contents of your hard-drive).  From this point of view you
are lucky it only printed some funny numbers rather than sending a killer sound
pulse from your speakers into your brain (though trying to wrap your head
around C semantics may produce a similar brain curdling result!).

The situation in other languages is quite different, for example using an
uninitialized variable in Ada can result in funny effects but the language
standard carefully delimits just how far they can go, and it's not that far.
As LLVM's "undef" is modelled on C's extreme behaviour, getting correct Ada
semantics in LLVM IR is rather tricky and in fact I didn't solve it yet.

Ciao, Duncan.

>
> 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.
>
>
> #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
>




More information about the llvm-dev mailing list