https://bugs.llvm.org/show_bug.cgi?id=33990

Tim Northover <t.p.northo...@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |t.p.northo...@gmail.com
             Status|NEW                         |RESOLVED
         Resolution|---                         |INVALID

--- Comment #2 from Tim Northover <t.p.northo...@gmail.com> ---
The program has undefined behaviour in this line:

    char *c = (char *)0 + foo();

The left-hand side of the addition is a null pointer, which you're not allowed
to perform arithmetic on.

The clause in the standard defines a few situations in which adding an integer
to a pointer produces a valid result and simply says "otherwise, the behaviour
is undefined". All of the actually defined cases start with a valid object
(which null isn't).

The definition of foo contains a similarly invalid subtraction.

I'm not sure if you've encountered undefined behaviour before but if not Chris
Lattner wrote a pretty good blog series on it a few years ago:
http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html

The short message is: avoid it at all costs. Its existence is what lets C and
C++ outperform most other languages but if you actually end up executing any
all bets are off (it's often referred to jokingly as "nasal demons" because it
would be perfectly permitted for the compiler to make demons fly out of your
nose according to the standard if your program has UB).

-- 
You are receiving this mail because:
You are on the CC list for the bug.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to