incrementing void *

  • Thread starter subramanian100in
  • Start date
A

Adrian Hawryluk

Keith said:
The best thing to do is to learn the rules and not use operators with
side effects *improperly* inside an expression (I'm not sure what you
mean by "equation"). For example, "x = y++;" is perfectly legitimate.
Even if you choose not to write code like that, you'll need to be able
to read and understand it.

Agreed, somewhat. Read my further post on using when not necessary
making difficult to read code.


Adrian


--
==========================================================
Adrian Hawryluk BSc. Computer Science
----------------------------------------------------------
Specialising in: OOD Methodologies in UML
OOP Methodologies in C, C++ and more
RT Embedded Programming
__--------------------------------------------------__
----- [blog: http://adrians-musings.blogspot.com/] -----
'--------------------------------------------------------'
My newsgroup writings are licensed under the Creative
Commons Attribution-Noncommercial-Share Alike 3.0 License
http://creativecommons.org/licenses/by-nc-sa/3.0/
==========================================================
 
P

pete

Adrian said:
Yes, but I didn't describe any here except for the '='.

I don't understand your meaning.
The assignment operator is not a sequence point.
The comma operator
has nothing to do with the posted code,
so there is no reason to treat the comma operator
differently from any other operators
which have nothing to do with the posted code.
 
A

Adrian Hawryluk

pete said:
I don't understand your meaning.
The assignment operator is not a sequence point.

Er, When evaluating an initializer. Not the '=' exactly.
The comma operator
has nothing to do with the posted code,

x = y++, y++;

Happy? This is a defined expression with a defined result.
so there is no reason to treat the comma operator
differently from any other operators

Actually, there is reason as some are defined as sequence points and
others are not.
which have nothing to do with the posted code.

I had assumed that you could follow the progression. My mistake.


Adrian
--
___________________________________________________________
/__________________Adrian_Hawryluk__BSc.____________________\
My newsgroup writings are licensed under the Creative
Commons Attribution-Noncommercial-Share Alike 3.0 License
______http://creativecommons.org/licenses/by-nc-sa/3.0/______
\________[blog: http://adrians-musings.blogspot.com/]_______/
 
P

pete

Adrian said:
Er, When evaluating an initializer. Not the '=' exactly.

I still don't see what you're talking about.
There is no initializer being evaluated in the posted code.
The expression (i = ++i + 1) is given as an example
of undefined behavior in the c99 standard.
If assignment was a sequence point,
there would be nothing wrong with that expression,
but assignment just simply isn't a sequence point.

x = y++, y++;

Happy? This is a defined expression with a defined result.

(x = y++ && y++) also a defined expression.
I don't understand why you said that the comma operator
is unique as an operator with a sequence point.
Actually, there is reason as some are defined as sequence points and
others are not.


I had assumed that you could follow the progression. My mistake.

There was no progression to follow.
 
R

Richard

Adrian Hawryluk said:
It can be if the car's wheels are apt to fall off, or the driver is
usually drunk. Yes, it is done, it can be done, it will be done. It
is just not always intuitive as to what is happening, and many people
learn a language by use and not reading a thick specification book of
it.

I am not illegitimatising the use of it, I am saying that it makes the
code that much more difficult to read. When you have to do it,
fine. When you do it whimsically without need, then you should just
enter the C Obfuscation contest.

Clarity in programming makes for more maintainable, understandable
code, reducing costs across the board.

You're wasting your time here with that argument. Certain people here
are so convinced of their skills that they think it will never be
necessary to run a debugger over their code. No joking. They appear to
have almost zero experience of working on huge codebases with legacy
support spread over several continents.
 
S

santosh

Chris said:
Is it not? The && introduces a sequence point. Am I missing
something?

#include <stdio.h>

int main(void) {
int x = 0, y = 0;
(x = y++ && y++);
printf("x == %d\ty == %d\n", x, y);
return 0;
}

$ ./0102
x == 0 y == 1
$
 
C

Chris Dollin

santosh said:
#include <stdio.h>

int main(void) {
int x = 0, y = 0;
(x = y++ && y++);
printf("x == %d\ty == %d\n", x, y);
return 0;
}

$ ./0102
x == 0 y == 1
$

Um, you missed out the explanation. What are you claiming
to demonstrate?
 
C

Clark Cox

No it isn't.

Of course it is. The && operator introduces a sequence point. The above
code is roughly equivalent to:

if(y++)
{
x = y++ != 0;
}
else
{
x = 0;
}

any compiler that tells you different is broken.
 
C

CBFalconer

Chris said:
Is it not? The && introduces a sequence point. Am I missing
something?

No, I am. :-( So the sequence of y (and thus x) values would be
something like:

-3 -1 +1 +3
or
-2 0 +1 +3

for which we must be able to dream up a use.
 
F

Francine.Neary

int x = 0, y = 0;
(x = y++ && y++);
printf("x == %d\ty == %d\n", x, y);

$ ./0102
x == 0 y == 1

I understand that as follows: first, && binds tighter than =, so we
have the statement
x= ( y++ && y++). Now let's what happens to the bit in parentheses.
The && is a "sequence point", so the first y++ is evaluated. The value
of this is the current value of y, which is 0. As a side effect, y is
incremented. So the situation now is that y==1, and the left-hand
operand of && is false. So && short-circuits, and the value of the
parenthesized expression is boolean false, i.e. 0. So x is set to 0.

For comparison, replace the initialization line with int x=0, y=1;
In this case, the value of y++ is 1, which is true (and as a side-
effect y becomes 2); so the RHS of && is evaluated, yielding the
current value of y, namely 2, which is again true. Again, y is
incremented as a a side-effect to 3. Then x is set equal to the result
of the parenthesized expression, which is true && true, i.e. boolean
true, i.e. 1.
 
R

R Pradeep Chandran

No it isn't.

I don't get it. Due to operator precedence, this is similar to

x = (y++ && y++);

Since the logical and operator has a sequence point after the
evaluation of the first argument, I do not see anything that can be
considered as undefined behavior.

Of course, the result is that x will have a value of 1 or 0 and y may
be incremented once or twice (depending on its initial value). That
does not make it undefined, does it?

Have a nice day,
Pradeep
 

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,781
Messages
2,569,616
Members
45,306
Latest member
TeddyWeath

Latest Threads

Top