Help me With this undefined behaviour.

S

Sumeet

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
 
A

Alf P. Steinbach

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

Richard Bos

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
 
L

Lew Pitcher

Ali said:
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)
 
B

Ben Pfaff

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?
 
J

Jason

Sumeet said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top