Sequence points

S

Senthil

Hi,

When standard states this
"
i = v[i++]; // the behavior is undefined
i = ++i + 1; // the behavior is undefined
"

Do both of the following code snippets produces undefined behaviour or
the second one alone.

int m = 2;
int answer = ++m * ++m + ++m * ++m;

or

int m = 2
m = ++m * ++m + ++m * ++m;

What are the sequence points inside these expressions?

Thanks,
Senthil
 
A

Artie Gold

Senthil said:
Hi,

When standard states this
"
i = v[i++]; // the behavior is undefined
i = ++i + 1; // the behavior is undefined
"

Do both of the following code snippets produces undefined behaviour or
the second one alone.

int m = 2;
int answer = ++m * ++m + ++m * ++m;

or

int m = 2
m = ++m * ++m + ++m * ++m;

What are the sequence points inside these expressions?

Thanks,
Senthil
They both produce UB, as there *are* no sequence points in either
expression.

HTH,
--ag
 
P

Pete Becker

Senthil said:
What are the sequence points inside these expressions?

There aren't any. There is a sequence point at the end of each one. The
behavior of both statements is undefined, because they modify the value
of m more than once without an intervening sequence point.
 
S

Senthil

Thanks Pete and Artie!!!

I read books and i read the standard..but i am still not able to
understand what a sequence point is ?
:(

Greetings,
Senthil
 
J

Jack Klein

Thanks Pete and Artie!!!

I read books and i read the standard..but i am still not able to
understand what a sequence point is ?
:(

Greetings,
Senthil

Sequence points:

1. The ';' at the end of a full expression.

2. The comma operator (but the commas that separate arguments in a
function call are NOT comma operators).

3. At the '?' in the ternary expression.

4. During a function call, after all the arguments are evaluated,
before the function begins executing.

5. When a function ends/returns or exits by throwing an exception.

6. At the evaluation of a && or || logical operator.

7. At the initialization of each base and member in a constructor
with an initialization list.
 

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