Help me With this undefined behaviour.

Discussion in 'C Programming' started by Sumeet, Jul 25, 2003.

  1. Sumeet

    Sumeet Guest

    I met a question in a test which invoked undefined behaviour and i was
    asked to answer the Expected answer of the question
    Specifications := Win98 Os Tc compiler
    int i=23;
    i=(i++|++i)^(i++ + ++i);

    what should be the value of i after this ? is there any way that i can
    predict the answer ? Also i don't have a good understanding of the
    Sequence points like when is the value of i incremented actually
    either immediately after i++ or after the ^ operator.Please refer me
    some useful links.
    Also i found that the answer to this question is different if i
    compiled it on gcc
    Sumeet, Jul 25, 2003
    #1
    1. Advertising

  2. On 25 Jul 2003 05:53:43 -0700, (Sumeet) wrote:

    >I met a question in a test which invoked undefined behaviour and i was
    >asked to answer the Expected answer of the question
    > Specifications := Win98 Os Tc compiler
    > int i=23;
    > i=(i++|++i)^(i++ + ++i);
    >
    >what should be the value of i after this ?


    As you noted it's undefined, or more precisely, underdetermined, for
    C or C++ in general. For the given compiler the answer can, however,
    be determined by checking the resulting program under various compiler
    options, under the assumption that the compiler will always produce
    the same program from the same source when given the same options.


    >is there any way that i can predict the answer ?


    No, but you can predict a set of answers that are more _likely_ than
    any arbitrary integer. For example, just from the initial value of
    23 and the limit of at most 2 increments in each parenthesis you
    know that an answer with at most 5 significant bits is exceedingly
    more likely than than a number >32.
    Alf P. Steinbach, Jul 25, 2003
    #2
    1. Advertising

  3. Sumeet

    Richard Bos Guest

    (Sumeet) wrote:

    > I met a question in a test which invoked undefined behaviour


    > what should be the value


    Those two sentences are contradictory. If the behaviour is undefined,
    there is nothing which _should_ happen. Undefined behaviour means
    exactly that: the Standard places no requirements on the behaviour of a
    program containing that construct. None. UB means "should" is out the
    window for good. Crashing is allowed; so is formatting your hard disk,
    or conjuring up a crowd of imps with whips to flog you into removing the
    undefined construct.

    Richard
    Richard Bos, Jul 25, 2003
    #3
  4. Sumeet

    Lew Pitcher Guest

    Ali Cinar wrote:
    > Hi
    >
    > S> int i=23;
    > S> i=(i++|++i)^(i++ + ++i);
    >
    > i = (23 | 25) ^ (25 + 27)


    Prove this assertion, please.

    [rest of proof snipped]




    --

    Lew Pitcher, IT Consultant, Application Architecture
    Enterprise Technology Solutions, TD Bank Financial Group

    (Opinions expressed here are my own, not my employer's)
    Lew Pitcher, Jul 25, 2003
    #4
  5. Sumeet

    Ben Pfaff Guest

    (Ali Cinar) writes:

    > S> int i=23;
    > S> i=(i++|++i)^(i++ + ++i);
    >
    > i = (23 | 25) ^ (25 + 27)


    Stop right there. What makes you think that the compiler will
    treat the expression that way?
    --
    int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
    );while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p\
    );}return 0;}
    Ben Pfaff, Jul 25, 2003
    #5
  6. Sumeet

    Jason Guest

    "Sumeet" <> wrote in message
    news:...
    > I met a question in a test which invoked undefined behaviour and i was
    > asked to answer the Expected answer of the question
    > Specifications := Win98 Os Tc compiler
    > int i=23;
    > i=(i++|++i)^(i++ + ++i);
    >
    > what should be the value of i after this ? is there any way that i can

    ....

    That's quite a despicable question really. I can't see any use on earth for
    an answer. Perhaps the people asking the question don't know about undefined
    behaviour. I would say the best answer is that the question is broken. I
    think it's easy to see why.

    The other thing is - it may well have different answers depending on the
    optimisation settings, did they supply those? just say no.
    Jason, Jul 25, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dan Cernat

    undefined behaviour

    Dan Cernat, Nov 11, 2003, in forum: C++
    Replies:
    1
    Views:
    343
    Josh Sebastian
    Nov 11, 2003
  2. marbac

    Undefined behaviour

    marbac, Jul 13, 2004, in forum: C++
    Replies:
    48
    Views:
    1,336
    Default User
    Jul 20, 2004
  3. Mantorok Redgormor
    Replies:
    70
    Views:
    1,751
    Dan Pop
    Feb 17, 2004
  4. VK
    Replies:
    45
    Views:
    591
    Dr John Stockton
    Sep 12, 2006
  5. -Lost
    Replies:
    13
    Views:
    368
    Richard Cornford
    Jan 31, 2007
Loading...

Share This Page