473,440 Members | 1,686 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,440 software developers and data experts.

a[i] = x[a[i]]++

BRG
Does anyone here know whether the assignment:

a[i] = x[ a[i] ]++;

is legal in C and, if so, what array element in x[] is post incremented?

That is, does the old or the new value of a[i] determine the index in
x[] of the post incremented array element.

I rather suspect the result is undefined - but am I right?

Brian Gladman
Nov 14 '05
54 2258
Old Wolf <ol*****@inspire.net.nz>:
Going by my formulation of the rule, the read of p
for (p->next = q) may occur after the write of p
in (p = q), so the behaviour is undefined.


You mean, the standard permits that the left operand (i.e. the
lvalue) of the assignment p->next = q is evaluated *after* the
evaluation of the result?

From draft ISO/IEC JTC1/SC22/WG14 N794:

6.3.16 Assignment operators

Syntax

[...]

Constraints

[...]

Semantics
[#3] An assignment operator stores a value in the object
designated by the left operand. An assignment expression
has the value of the left operand after the assignment,
but is not an lvalue. [...] The side effect of updating
the stored value of the left operand shall occur between
the previous and the next sequence point.

[#4] The order of evaluation of the operands is
unspecified.

The working group regarded the order of evaluation of the
operands as worth to be mentioned ([#4]) but left open the less
obvious possibility, that the left operand (i.e. the lvalue)
might not be computed until the result has been computed and used
in another assignment?

[#3] permits that the "side effect of updating the stored value"
may occur late, but it does not say anything about the time, when the
lvalue itself is computed.

Any comments?
Nov 14 '05 #51
In article <cs*********@news4.newsguy.com>,
Chris Torek <no****@torek.net> wrote:
"If the Standard says that the result depends on the phase of the
moon, the programmer should be prepared to look out the window as
necessary." --me


This would make phase-of-the-moon programs either trivial or
fiendishly complicated.

-- Richard
Nov 14 '05 #52
Andrey Tarasevich <an**************@hotmail.com>:
Albert van der Horst wrote:
Now the above is using the side effect,
to mix up in one statement
new = x[ old ];
x[ old ] += 1;

It is *not* equivalent to
a[i] = x[ a[i] ] += 1;
because, being a post-increment, the original value of
x[ a[i] ] must be used.
This is completely irrelevant. If you read this thread
carefully, you'll see that the real question is which value of
'a[i]' is used (old or new), not which value of 'x[a[i]]' is
used. The latter is pretty much irrelevant. The issue with 'a[i]' is present in your version of the
expression just like it is present in the original version. You
haven't changed anything important by replacing post-increment
with compound assignment.


I think, he has: If you replace x[ a[i] ]++ with
(x[ a[i] ] += 1), the following rule regarding the += applies:

From ISO/IEC JTC1/SC22/WG14 N794:

6.3.16.2 Compound assignment

[...]

Semantics

[#3] A compound assignment of the form E1 op= E2
differs from the simple assignment expression
E1 = E1 op (E2) only in that the lvalue E1 is evaluated
only once.

Therefore, in a[i] = (x[ a[i] ] += 1) the prior value of a[i] is
accessed only once before the new value is stored in a[i],
because 6.3.16.2[#3] garanties that x[a[i]] is not evaluated a
second time. Or I am wrong?
Nov 14 '05 #53
Albert van der Horst wrote:
Peter Nilsson <ai***@acay.com.au> wrote:
Albert van der Horst wrote:
BRG <br*@nowhere.org> wrote:
> Does anyone here know whether the assignment:
>
> a[i] = x[ a[i] ]++;

I never use a statement like
x++;
on its own.
Why?!
I always do the equivalent (barring the result value)
x += 1;
This shows that it is an assignment, rather than a side effect.


Rather poor reasoning since x++ is idiomatic, and because _all_
statements are _only_ evaluated for their side effects,
assignments or otherwise (e.g. function calls.)


You use side-effect in a politically correct way here.


It's a term defined in the standard...

"Accessing a volatile object, modifying an object, modifying a
file, or calling a function that does any of those operations
are all _side effects_, ..."
I thought the context made clear that is not what I say.
In an expression like (i + j++)
two thing are happening with j, getting its value and the
incrementation which I call a side-effect. (And the use is
not improper, because it is indeed a lasting effect.).
I (think) I understand that. I was pointing out that C turned
assignments and function calls into expression operators. [It's
not the only language to do this, but the consequences do
surprise many people who program other languages.]
(The result of x++ is of course x, then it is thrown away,
oh and by the way, I have incremented x for you.
Extremely silly.)


Yes, ": @++ dup @ dup 1+ over ! ;" _is_ silly, but only because
Forth is silly. ;)


This leaves a pointer to x and has its value incremented.


Drat! I meant ": @++ dup @ dup 1+ rot ! ;"
It could be simplified to: @++ 1 over +! ;
I was hoping you would simplify it, although I didn't anticipate
getting my version wrong. ;( But I'm pretty sure what I was
aiming for could be simplified to... : @++ 1 over +! @ ;

Anyway, my point is that, to a Forth programmer, the optimisation
seems obvious. Within C, the same 'obviousness' surrounds the
optimisation of x += 1 to x++; or ++x;.
It is not equivalent to the c-code x += 1;
It was meant to be equivalent to x++;, i.e. (adr -- n) where
adr is the lvalue (address) of x.
Lets discuss @+ which leaves an incremented address
and a content, the equivalent of *p++ in c.

In the context of Forth it is indeed silly to
do
@+ DROP
instead of just
@
This is analogous.
In C it depends on the context: *p++; is silly, however
c = *p++; or f(*p++); isn't.
In the first example you calculate
two results, then throw one away. Instead of just calculating
a result.

But more on topic.
Is my
x += 1;
sufficiently un-idiomatic to get some eyes browsed?
As Keith points out, there are subjective cases where it's valid,
but in general it will raise eyebrows.

It will make the reader think that you're entirely clued up
in C, just as my posted attempt at Forth was obviously naive
from a Forth perspective.
What about
p += sizeof(struct x);


[Again See Keith's comment, but also....]

Note that sizeof(struct x) cannot generally be expected to be 1.
There is no unary auto-increment-by-something-other-than-1
operator in C. So, p += 2; is not un-idiomatic.

--
Peter

Nov 14 '05 #54
Richard Bos wrote:

Christian Kandeler <ch****************@hob.de_invalid> wrote:
Old Wolf wrote:
the standard is quite clear that in (a = b = 3.1),
a gets the value 3.1 even if b were an int.


Really? You should tell that to the gcc people, then:


Or to the C Standard committee:

# The type of an assignment expression is the type of the left operand
# unless the left operand has qualified type, in which case it is the
# unqualified version of the type of the left operand.
(From 6.5.16#3. Identical wording in C89.)


What happens is:
a gets (double)(int)3.1
b gets (int)3.1
and the assignments are made in no particular order.

--
pete
Nov 14 '05 #55

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

Similar topics

0
by: Arthur T. Murray | last post by:
A Python AI Weblog for coding AI mind-modules is at http://mentifex.virtualentity.com/python.html (q.v.). The theory and practice of artificial intelligence have advanced sufficiently that it is...
303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
0
by: Yaroslav Bulatov | last post by:
The list of Python AI/Machine Learning resources has been recently expanded. http://yaroslav.hopto.org/pubwiki/index.php/ai-python It's a wiki page, so feel free to add to it Yaroslav
9
by: Nick | last post by:
Hi, I recently went to an art exhibition, and one of the artists had an AI engine projected onto a screen with a keyboard for visitors to type questions in with. Curiously, I asked the artist...
19
by: Arthur T. Murray | last post by:
A C++ AI Weblog for implementing artificial intelligence is at http://mentifex.virtualentity.com/cpp.html -- with C++ source. Arthur T. Murray -- http://doi.acm.org/10.1145/307824.307853 -- ACM...
40
by: Allan M. Bruce | last post by:
I am applying for my first jobs after completing my PhD. I have been asked by a company to go and take a C programming test to see how my C skills are. Apparantly this test is mostly finding...
18
by: Ed Zagmoon | last post by:
I want to write an evil AI in C++ that will be able to evolve and become a dangerous computer-mastermind, something like Skynet. How can I write such an AI in C++ and how can I make it hate...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.