Discussion:
Dumb C question
(too old to reply)
Chris Saunders
2010-01-19 04:41:42 UTC
Permalink
Sorry for asking this - I haven't been using C for a long time and I'm
adapting some C code to another language. Here is a line from the C code
I'm adaptinge:

x = mt[mti++];

Is x set to mt[mti] or mt[mti + 1]. after this statement is executed.

Regards
Chris Saunders
Scott McPhillips [MVP]
2010-01-19 05:06:10 UTC
Permalink
Post by Chris Saunders
Sorry for asking this - I haven't been using C for a long time and I'm
adapting some C code to another language. Here is a line from the C code
x = mt[mti++];
Is x set to mt[mti] or mt[mti + 1]. after this statement is executed.
Regards
Chris Saunders
x = mt[mti];

Then the ++ is performed.
--
Scott McPhillips [VC++ MVP]
Chris Saunders
2010-01-19 05:32:58 UTC
Permalink
Thanks for the reply Scott. Unfortunatly your answer was not clear to me.
Perhaps my question wasn't clear enough. I'll try to be clearer. Is the ++
performed before or after x is set? I don't think x = mt[mti++] is the same
as x = mt[++mti] is it?

Regards
Chris Saunders
Post by Scott McPhillips [MVP]
Post by Chris Saunders
Sorry for asking this - I haven't been using C for a long time and I'm
adapting some C code to another language. Here is a line from the C code
x = mt[mti++];
Is x set to mt[mti] or mt[mti + 1]. after this statement is executed.
Regards
Chris Saunders
x = mt[mti];
Then the ++ is performed.
--
Scott McPhillips [VC++ MVP]
Chris Saunders
2010-01-19 06:02:07 UTC
Permalink
I re-read your reply Scott and got it this time. Thanks again.

Regards
Chris Saunders
Post by Chris Saunders
Thanks for the reply Scott. Unfortunatly your answer was not clear to me.
Perhaps my question wasn't clear enough. I'll try to be clearer. Is the
++ performed before or after x is set? I don't think x = mt[mti++] is the
same as x = mt[++mti] is it?
Regards
Chris Saunders
Post by Scott McPhillips [MVP]
Post by Chris Saunders
Sorry for asking this - I haven't been using C for a long time and I'm
adapting some C code to another language. Here is a line from the C
x = mt[mti++];
Is x set to mt[mti] or mt[mti + 1]. after this statement is executed.
Regards
Chris Saunders
x = mt[mti];
Then the ++ is performed.
--
Scott McPhillips [VC++ MVP]
Barry Schwarz
2010-01-31 00:53:25 UTC
Permalink
On Tue, 19 Jan 2010 00:32:58 -0500, "Chris Saunders"
Post by Chris Saunders
Thanks for the reply Scott. Unfortunatly your answer was not clear to me.
Perhaps my question wasn't clear enough. I'll try to be clearer. Is the ++
performed before or after x is set? I don't think x = mt[mti++] is the same
as x = mt[++mti] is it?
In your original example, mti++ is evaluated. The result is the
current unmodified value of mti. Then two additional things happen. A
value is stored in x and the incremented value is stored in mti. These
last two can happen in any order the compiler chooses.

In your second example, ++mti is evaluated. The result is the current
value of mti+1. Then two additional things happen. A (different)
value is stored in x and the incremented value is stored in mti. These
last two also can happen in any order the compiler chooses.

The pre- and post-fix increment operators do not tell you when the
operand is incremented. They only tell you whether the result of the
evaluation is the incremented value or not.
--
Remove del for email
Ulrich Eckhardt
2010-01-19 08:24:11 UTC
Permalink
Post by Scott McPhillips [MVP]
Post by Chris Saunders
x = mt[mti++];
Is x set to mt[mti] or mt[mti + 1]. after this statement is executed.
Regards
Chris Saunders
x = mt[mti];
Then the ++ is performed.
Actually, no. The ++ is performed first, as part of evaluating 'mti++'.
However, the result of that expression is the former value of mti, i.e. the
one before evaluating that expression.


#include <stdio.h>
int g = 0;
void foo(int x)
{
printf("x=%d, g=%d\n", x, g);
}
int main()
{
foo(g++);
}
// output: x=0, g=1

Note: I'm not 100% sure if this is a proof, it might actually invoke
implementation-specific behaviour or even undefined behaviour. However, the
point I was trying to make was that the definition of the postfix increment
operator rather involves storing the original value in a temporary and not
postponing the increment operation. This isn't visible in the original
example though.

Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
Igor Tandetnik
2010-01-19 18:11:05 UTC
Permalink
Post by Ulrich Eckhardt
#include <stdio.h>
int g = 0;
void foo(int x)
{
printf("x=%d, g=%d\n", x, g);
}
int main()
{
foo(g++);
}
// output: x=0, g=1
Note: I'm not 100% sure if this is a proof, it might actually invoke
implementation-specific behaviour or even undefined behaviour.
Looks OK to me. There is a sequence point after all function parameters are evaluated, and before the function's body is entered. All side effects from parameter evaluation must compete by then.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
Continue reading on narkive:
Loading...