473,414 Members | 1,623 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Validity of several ++operators on the same line

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

Jan 16 '07 #1
11 1297
On Jan 16, 1:25 pm, "Kufa" <pota...@gmail.comwrote:
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.

--
Erik Wikström

Jan 16 '07 #2
Kufa wrote:
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
Jan 16 '07 #3
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

Jan 16 '07 #4
Kufa wrote:
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.
Jan 16 '07 #5
So to sum up and check i did not misunderstand you, it's legal, but
undefined behaviour ?

Thanks for your answers!

/Kufa

Jan 16 '07 #6
"Kufa" <po*****@gmail.comwrote in news:1168952389.509578.101930@
38g2000cwa.googlegroups.com:
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.
Jan 16 '07 #7
Kufa wrote:
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.
Jan 16 '07 #8

Kufa wrote:
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.
"Undefined behavior" means the program is not well formed, hense
illegal.

Jan 16 '07 #9
Noah Roberts wrote:
Kufa wrote:
>>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.

"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)
Jan 16 '07 #10
Noah Roberts wrote:
Kufa wrote:
>>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.

"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.
Jan 17 '07 #11
Kufa wrote:
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.

--
Ivan
http://www.0x4849.net
Jan 18 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Lionel B | last post by:
Greetings, I am trying to implement "element-wise" arithmetic operators for a class along the following lines (this is a simplified example): // ----- BEGIN CODE ----- struct X { int a,b;
7
by: Michael Sgier | last post by:
Hi folks class X { private: CTexture *modelTex; // texture data } CTexture* X::Load()
16
by: jacob navia | last post by:
Valid pointers have two states. Either empty (NULL), or filled with an address that must be at a valid address. Valid addresses are: 1) The current global context. The first byte of the data...
6
by: jas_lx | last post by:
The basic understanding of what bitwise operators (& ^ | >> << ) comes fairly simple, as long as one has a fundamental understanding of bits, bytes and binary. Having done some Win32...
6
by: Protoman | last post by:
I'm writing a program to calc truth tables of arguments to prove that they are logically valid. Currently, I have to tell the program to print a truth table, and check it by hand to prove it's...
33
by: a | last post by:
Hi, I have a pointer that points to an unknown heap memory block, is it possible to check the pointer + 3 is valid or not? If it is impossible, how can I do the check? Thanks
9
by: Andy Dingley | last post by:
Here's a chunk of a longer piece of punditry I'm working on, re: the choices between doctypes for authoring and the State of the Union for validity. I've got a bucketload of nasty code and a bunch...
18
by: Zach | last post by:
Can someone list the various macro operators and what they mean. Came across a function macro: #define max(a, b) ((a)>(b)?(a):(b)) What does "?" amd ":" mean in this statement? Zach
9
by: xhe | last post by:
Hi, I need to program to check the validity of IP address through PHP Initially I used this one: $url="http://www.ntc.gov.au/ViewPage.aspx? page=A02400304500100020"; $fp=fopen($url,"r");...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.