Validity of several ++operators on the same line

K

Kufa

Hello,

I am wondering of the validity of those lines, not meaning i'm using
them, but i cant point out in the norm if they are legal, and in such a
case the expected behaviour.

int a = 2;
int b = (++a) * ((++a)+2);
printf( "%d %d", ++b, ++b );


They compiled in Commeau, but that doesn't mean it's legal :)
Any help or suggestion is welcome!
Thanks,

/kufa
 
K

Kai-Uwe Bux

Kufa said:
I am wondering of the validity of those lines, not meaning i'm using
them, but i cant point out in the norm if they are legal, and in such a
case the expected behaviour.

int a = 2;
int b = (++a) * ((++a)+2);
printf( "%d %d", ++b, ++b );

The behavior is undefined as per clause [5/4] of the standard.


Best

Kai-Uwe Bux
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

Hello,

I am wondering of the validity of those lines, not meaning i'm using
them, but i cant point out in the norm if they are legal, and in such a
case the expected behaviour.

int a = 2;
int b = (++a) * ((++a)+2);
printf( "%d %d", ++b, ++b );

They compiled in Commeau, but that doesn't mean it's legal :)
Any help or suggestion is welcome!

No, they are not legal. If in doubt just try to evaluate the expression
in your head. Remember that there are no rules that say the just
because A comes before B in an expression A will be evaluated first.
 
K

Kufa

No, they are not legal. If in doubt just try to evaluate the expression
in your head. Remember that there are no rules that say the just
because A comes before B in an expression A will be evaluated first.

Well i'd think this is legal with undefined behaviour. Can you point me
somewhere in the norm that'd say it's not legal, really can't find
anything.

Thanks,

/kUfa
 
R

Rolf Magnus

Kufa said:
Hello,

I am wondering of the validity of those lines, not meaning i'm using
them, but i cant point out in the norm if they are legal, and in such a
case the expected behaviour.

int a = 2;

int b = (++a) * ((++a)+2);

This one has undefined behavior, because a is modified twice without a
sequence point in between.
printf( "%d %d", ++b, ++b );

Undefined too, similar to the previous one.
They compiled in Commeau, but that doesn't mean it's legal :)

Well, it's not a syntacital error. A compiler doesn't need to complain about
it.
 
K

Kufa

So to sum up and check i did not misunderstand you, it's legal, but
undefined behaviour ?

Thanks for your answers!

/Kufa
 
A

Andre Kostur

So to sum up and check i did not misunderstand you, it's legal, but
undefined behaviour ?

That depends on what you define as "legal". In my definition, "legal" code
doesn't invoke Undefined Behaviour. If by "legal" you mean "syntactically
correct", then yes it's legal.
 
R

red floyd

Kufa said:
So to sum up and check i did not misunderstand you, it's legal, but
undefined behaviour ?

Thanks for your answers!

/Kufa
Kai-Uwe Bux already told you where to look in the standard. 5/4.

If you are unfamiliar with that notation, it means paragraph 4 of
section 5, not section 5.4.
 
P

Pete Becker

Noah said:
"Undefined behavior" means the program is not well formed, hense
illegal.

Does that mean I'll be arrested for writing code like that?

Seriously, according to the standard, programs are well-formed or
ill-formed, and some well-formed programs have undefined behavior. It
doesn't say anything about legal or illegal. As always, using undefined
terms leads to confusion.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
R

Ron Natalie

Noah said:
"Undefined behavior" means the program is not well formed, hense
illegal.
Nope, "undefined behavior" does not mean the program is not well
formed. An ill-formed program requires a diagnostic. UB does
not.
 
I

Ivan Novick

Kufa said:
So to sum up and check i did not misunderstand you, it's legal, but
undefined behaviour ?

Thanks for your answers!

/Kufa

To sum up, if you write code that changes the same variable twice on the
same line you are not guaranteed what the code will do. i.e. its code
that should not be written. Also if you add -Wall to your gcc compile
it will warn you against doing this.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top