[cfe-dev] Undefine behaviour. Can one explain me this code.

mats petersson mats at planetcatfish.com
Sat May 23 15:08:02 PDT 2015


You are using the value that you are incrementing in the same sequence
point. C and C++ does not define whether (a < a++) is the same as a < a,
a++, or a++, a < a, or some other variation on that theme (and no, it
doesn't help if you do (a < b++ || b < a++), it's still undefined what
order the ++ and greater than happens in.

It is only OK if you do them separately, in different sequence points (if
sequence point is a mystery to you, google it, as my explanation will not
be as good as what you find in google). The only thing that IS defined is
that a is one more AFTER the sequence point.

This is just one example of such undefined behaviour. Both C and C++ are
languages that have a lot of "undefined behaviour", where it is up to the
implementor of the compiler and the hardware exactly what happens
(including completely unexpected behaviour, such as the compiler starts a
game, turning off the power, rebooting or the generated code crashes for
things that you don't expect to crash). Included in "undefined behaviour"
is "it works as you expect", but it also covers everything else that
computer is capable of possibly doing. [Of course, compiler people are
typically kind (or at least not evil) and try their best effort to make the
undefined behaviour as "nice" and close to what you would expect as
possibly can, as long as it doesn't harm performance for defined behaviour]

--
Mats

--
Mats

On 23 May 2015 at 22:29, umang777bhatt <umang777bhatt at gmail.com> wrote:

> int main()
> {
> int a=012;
> int b=05;
> if(a<++a||b<++b)
> printf("DJ");
> else
> printf("%d Welcome %d",a,b);
> }
>
>
>
> --
> View this message in context:
> http://clang-developers.42468.n3.nabble.com/Undefine-behaviour-Can-one-explain-me-this-code-tp4045706.html
> Sent from the Clang Developers mailing list archive at Nabble.com.
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150523/1c644acb/attachment.html>


More information about the cfe-dev mailing list