Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Is a[i] = i++ correct?

Reply
Thread Tools

Is a[i] = i++ correct?

 
 
CBFalconer
Guest
Posts: n/a
 
      12-28-2007
Kaz Kylheku wrote:
> gaze...@xmission.xmission.com (Kenny McCormack) wrote:
>
>> Get off the "the C standard says..." horse and think logically,
>> like a human being for a second, and it becomes clear. When a
>> human sees the above, they logically think:

>

.... snip ...
>
>> in that order. So, obviously, if i=0 on start, then at end,
>> a[1] will

>
> Obviously, you're a moron.


No, he's a known troll. PLONK it.

--
Merry Christmas, Happy Hanukah, Happy New Year
Joyeux Noel, Bonne Annee, Frohe Weihnachten
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
 
 
 
Flash Gordon
Guest
Posts: n/a
 
      12-29-2007
Rick wrote, On 28/12/07 23:25:
> On Fri, 28 Dec 2007 20:17:30 +0000, Flash Gordon
> <> wrote:
>
>>> I hope I have corrected your misconceptions.

>
> Good evening, Flash.
>
> Respectfully, there were no misconceptions.


There were I'm afraid, and the FAQ addresses them.

> There were only partial
> answers.


They were incorrect answers because they specified a limited number of
possibilities.

> I gave Jeniffer the answers that I thought were needed
> without getting into a lot of detail that goes way beyond the scope of
> my perception of the questions asked. Of course, my perception may
> have been wrong.


Your perception is wrong.

The standard *explicitly* states that the behaviour is undefined. The
standard also explains what it means by "undefined behaviour". It says
that undefined behaviour is behaviour upon which it poses no
requirements. I.e. as far as the language standard is concerned, if the
program invokes undefined behaviour literally *anything* that happens is
acceptable.

Now to give a possible example of *why* you could get something as
unexpected as a crash. Imagine a processor which can do multiple
operations in parallel (many processors have been able to do this for
years). Further imagine that it has multiple data paths, so it is
possible for it to read from one (cached) location at the same time as
writing to another. Now, the compiler knows that it does not have to
worry about you reading a variable at the same time as writing to it
because you can only read from it as part of generating the new value.
So it does minimal analysis and writes code that does...
Increment the location that contains i and in parallel read it in to
an index register.
Unfortunately on the processor I'm suggesting this is not possible since
it cannot simultaneously do an increment on a location and read it. So
this generates an "illegal parallel operation" exception which the C
runtime does not catch (because there is no "legal" way to generate it)
and your program crashes.

I've not used a processor precisely like the one I've described, but I
have used processors which can do things in parallel but only if the
combination of requested operations is valid and some of the
combinations were quite surprising.
--
Flash Gordon
 
Reply With Quote
 
 
 
 
Charlton Wilbur
Guest
Posts: n/a
 
      12-29-2007
>>>>> "R" == Rick <> writes:

R> Good evening, Keith.

R> If...

R> i = 2; a[ i ] = i++;

R> ... then I claim that a[ i ] will be either a[ 2 ] or a[ 3 ],
R> depending on whether i++ gets evaluated first or last, but it
R> must be either one or the other.

R> Wrong?

Wrong. a[i] = i++; is explicitly undefined behavior; you've been
pointed at the FAQ entry that explains this in detail, so it's not
worth going into here. As soon as you write a[i] = i++; the compiler
is free to do whatever it wants with the rest of your program. It
could decide to fill the entire a array with junk, or halt because it
detected undefined behavior at runtime.

And seriously, even if it were somewhat defined, and you knew that
that statement wound up being at least one of the following, depending
on quirks of the compiler --

a[2] = 3;
a[3] = 3;
a[2] = 2;

-- why not just write the one you want? That way it will produce the
same results on all compilers, which is the point of a portable
programming language. a[i] = i+1; is *much* clearer, and has the
significant advantage of only one plausible interpretation.

Charlton


--
Charlton Wilbur

 
Reply With Quote
 
Chris Torek
Guest
Posts: n/a
 
      12-29-2007
In article <>
Rick <> wrote:
>If...
>
>i = 2;
>a[ i ] = i++;
>
>... then I claim that a[ i ] will be either a[ 2 ] or a[ 3 ],
>depending on whether i++ gets evaluated first or last, but it must be
>either one or the other.


What if they are done simultaneously?

If you are familiar with the Itanium, or with "out of order"
execution on various CPUs, consider the kind of CPU called a "VLIW",
or "Very Long Instruction Word", CPU. (The Itanium is actually a
sort of "hybrid VLIW", as it were.)

A VLIW CPU has multiple functional units (just like most modern
pipelining processors), but instead of feeding and controlling them
semi-automatically by reading a sequence of ostensibly linear
instructions -- "do this step, then when it is finished, do that
step, then when that is finished, do a third step" -- and attempting
to assemble them into multiple simultaneous actions, a VLIW CPU
simply reads a very long instruction word that gives a bunch of
actions to take simultaneously.

For instance, to do something like:

total = val1 * 1.07 + val2 * 3 + array[index];

on a conventional CPU, we might get:

shl %r4, 2, %r7 # %r4 = index (in %r7) * 4
load %f1, %r3(%r7) # sets %f1 to array[index]
load %f4, CONSTANT_POOL_3_0(%rPOOL)
mul %f4, %f5, %f4 # set %f4 to val2 * 3.0
load %f3, CONSTANT_POOL_1_07(%rPOOL)
mul %f3, %f2, %f3 # set %f3 to val1 * 1.07
add %f3, %f4, %f3 # %f3 = %f3 + %f4
add %f1, %f1, %f3 # %f1 = %f1 + %f3
stor %f1, addr(total) # total = <result>

for a total of 9 instructions. On the VLIW, however, we might
get something more like:

block {
%r4 = (%r7 << 2) + %r3 # actually uses two integer units
# (output of shifter fed as input
# to adder)
%f4 = CONSTANT_POOL_3.0 # one load/store unit
%f3 = CONSTANT_POOL_1.07 # another load/store unit
# four units run simultaneously
}
# wait for results
block {
%f1 = load(%r4) # one load/store unit
%f4 *= %f5 # one FPU unit
%f3 *= %f2 # another FPU unit
# three units run simultaneously
}
# wait for results
block {
%t1 = %f1 + %f3 + %f4 # actually uses two FPU units (output
# of adder1 fed as input to adder2)
store(%t1, addr(total)) # one load/store unit (using output of adder2)
}

for a total of 3 instructions.

The tricky part occurs when the VLIW compiler is fed code with
undefined behavior like a[i] = i++ -- we end up with an "invalid"
block that reads:

block {
%t1 = (%r7 << 2) + %r3 # a[i], with a in %r3 and i in %r7
store(%r7, %t1)
%r7 += 1
}

which tries to use one load/store unit to store the old value of
i (in %r7) to a[i] (computed into the entity denoted %t1 here,
although really we just route the output of the adder into the
load/store unit) while at the same time trying to use a third
integer unit to increment %r7. Since registers have three or more
read ports and one write port, this is OK electrically, but the
result is unpredictable because the timing of the output of the
adder-and-shifter ALUs is not defined with respect to the timing
of the incrementing ALU and the timing of the load-store unit.

On early implementations, you get one a different answer from
intermediate implementations. Because debugging this turns out to
be so difficult, the final implementation of the chip includes
circutry to detect the "collision", causing a runtime trap. The
effect of a[i] = i++ then becomes:

illegal instruction - core dumped

rather than setting a[2] or a[3] to either 2 or 3.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-29-2007
Charlton Wilbur <> writes:
>>>>>> "R" == Rick <> writes:

>
> R> Good evening, Keith.
>
> R> If...
>
> R> i = 2; a[ i ] = i++;
>
> R> ... then I claim that a[ i ] will be either a[ 2 ] or a[ 3 ],
> R> depending on whether i++ gets evaluated first or last, but it
> R> must be either one or the other.
>
> R> Wrong?
>
> Wrong. a[i] = i++; is explicitly undefined behavior; you've been
> pointed at the FAQ entry that explains this in detail, so it's not
> worth going into here. As soon as you write a[i] = i++; the compiler
> is free to do whatever it wants with the rest of your program. It
> could decide to fill the entire a array with junk, or halt because it
> detected undefined behavior at runtime.
>
> And seriously, even if it were somewhat defined, and you knew that
> that statement wound up being at least one of the following, depending
> on quirks of the compiler --
>
> a[2] = 3;
> a[3] = 3;
> a[2] = 2;
>
> -- why not just write the one you want? That way it will produce the
> same results on all compilers, which is the point of a portable
> programming language. a[i] = i+1; is *much* clearer, and has the
> significant advantage of only one plausible interpretation.


The expression a[i] = i++ clearly *intends* to do two different
things; it modifies an element of the array a, and it increments i.
None of your proposed replacements modify i.

If it were well-defined, it could make sense to use a[i] = i++ in a
loop to set a[0] to 0, a[1] to 1, etc. If you want to achieve the
intended effect, you need to separate the modification of a and of i
with a sequence point.

--
Keith Thompson (The_Other_Keith) <kst->
[...]
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Golden California Girls
Guest
Posts: n/a
 
      12-29-2007
Flash Gordon wrote:
> Rick wrote, On 28/12/07 23:25:
>> On Fri, 28 Dec 2007 20:17:30 +0000, Flash Gordon
>> <> wrote:
>>
>>>> I hope I have corrected your misconceptions.

>>
>> Good evening, Flash.
>>
>> Respectfully, there were no misconceptions.

>
> There were I'm afraid, and the FAQ addresses them.
>
>> There were only partial
>> answers.

>
> They were incorrect answers because they specified a limited number of
> possibilities.
>
>> I gave Jeniffer the answers that I thought were needed
>> without getting into a lot of detail that goes way beyond the scope of
>> my perception of the questions asked. Of course, my perception may
>> have been wrong.

>
> Your perception is wrong.
>
> The standard *explicitly* states that the behaviour is undefined. The
> standard also explains what it means by "undefined behaviour". It says
> that undefined behaviour is behaviour upon which it poses no
> requirements. I.e. as far as the language standard is concerned, if the
> program invokes undefined behaviour literally *anything* that happens is
> acceptable.


So when it is run, it empties your bank account and transfers it all to mine.

So I'm sure we will hear about a complier that some undergrad wrote in some
class that does something other than evaulate one side or the other first, or
some obscure chip that has some strange instruction that this one shot compiler
emits that causes all havoc and stops every train in Boston. No one cares
because they aren't asking about that specific implementation defined example!

Post a table about what the top ten most used compliers do when generating code
for the top ten most used chips and someone might consider it useful information.
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      12-29-2007
Golden California Girls <GldnCAG...@aol.com.mil> wrote:
> Flash Gordon wrote:
> > ... The standard *explicitly* states that the behaviour
> > is undefined. ...

>
> So I'm sure we will hear about a complier that some
> undergrad wrote in some class that does something other
> than evaulate one side or the other first, ...
> ... No one cares because they aren't asking about that
> specific implementation defined example!


True. If they're in comp.lang.c, then they should only
be interested in what happens on the virtual C machine.

> Post a table about what the top ten most used compliers
> do when generating code for the top ten most used chips
> and...


That is precisely what happens in misguided texts and
other forums. The result is people who think all the world
is intel, or they think they've discovered a compiler bug
when ill-formed expressions don't do what they expect.

> someone might consider it useful information.


Sadly, too many people do.

--
Peter
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      12-29-2007
Keith Thompson wrote:
>

.... snip ...
>
> The expression a[i] = i++ clearly *intends* to do two different
> things; it modifies an element of the array a, and it increments
> i. None of your proposed replacements modify i.
>
> If it were well-defined, it could make sense to use a[i] = i++ in
> a loop to set a[0] to 0, a[1] to 1, etc. If you want to achieve
> the intended effect, you need to separate the modification of a
> and of i with a sequence point.


Of course it is trivially easy to rewrite the statement to do
something accurately defined. For instance:

a[i] = i + 1; i++;
or
a[i + 1] = i; i++;

--
Merry Christmas, Happy Hanukah, Happy New Year
Joyeux Noel, Bonne Annee, Frohe Weihnachten
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      12-29-2007
Kenny McCormack wrote:

> In article <rd1dj.112184$> ,
> Chris Dollin <> wrote:
>>jeniffer wrote:
>>
>>> I want to know why is a[i] = i++ ; wrong? People say that it is
>>> because of different parsing during compilation.Please explain
>>> technically why it is wrong/behaviour undefined?

>>
>>Even supposing it were not specifically undefined (ie, the C
>>Standard says that it doesn't care what this code does), what
>>would you expect it to /mean/?

>
> Get off the "the C standard says..." horse and think logically, like a
> human being for a second,


Humans don't generally think logically; this is a Good Thing
except in unusual circumstances.

> and it becomes clear. When a human sees the
> above, they logically think:
> 1) Evaluate the RHS
> 2) Assign it to the LHS
> in that order.


No, they don't. Humans who have been /taught/ to do this will
do this; humans who have not, won't. Logic has nothing to do
with it.

Another "logical" order is to evaluate the LHS (for its address)
first, then to evaluate the RHS. "Logically" one does everything
left-to-right, in "logical" reading order.

>>Let's not mention that the intention of the writer of that
>>code is completely unclear.

>
> Wrong. See above.


We don't know who wrote the snippet in question, so we have no
idea what their intent was; your assertion that they "think
logically" and do right-to-left evaluation has no evidence.

Good try.

--
Sarky Hedgehog
"Our future looks secure, but it's all out of our hands"
- Magenta, /Man and Machine/

 
Reply With Quote
 
Golden California Girls
Guest
Posts: n/a
 
      12-29-2007
Peter Nilsson wrote:
> Golden California Girls <GldnCAG...@aol.com.mil> wrote:
>> Flash Gordon wrote:
>>> ... The standard *explicitly* states that the behaviour
>>> is undefined. ...

>> So I'm sure we will hear about a complier that some
>> undergrad wrote in some class that does something other
>> than evaulate one side or the other first, ...
>> ... No one cares because they aren't asking about that
>> specific implementation defined example!

>
> True. If they're in comp.lang.c, then they should only
> be interested in what happens on the virtual C machine.
>


Yes, an imaginary compiler on an imaginary chip! Must be why there are so many
moron taliban ass kissing trolls just to quote the last couple of days of
messages. People's imaginations must not match, maybe a vulcun mind meld is
where the group should meet rather than on usenet. It would be as useful to the
real world as what happens now.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57