Hi
I want to know why is a[i] = i++ ; wrong? People say that it is
because of different parsing during compilation.Ple ase explain
technically why it is wrong/behaviour undefined?
Regards,
Jeniffer 14 2174
jeniffer said:
Hi
I want to know why is a[i] = i++ ; wrong?
What do you think it should mean? Given this code:
int a[3] = { 5, 7, 9 };
i = 0;
a[i] = i++; /* bug */
which member of a[] do you think will be updated, and to what value?
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
On Dec 28, 7:50*pm, gaze...@xmissio n.xmission.com (Kenny McCormack)
wrote:
>
christian.bau <christian....@ cbau.wanadoo.co .ukwrote:
Java programmers all over the world think you are completely wrong. In
Java, a [i] = i++; has defined behaviour. The expression is evaluated
from left to right. So first it evaluates the lvalue a [i], then the
right hand side i++. The array element changed is determined by the
original value of i.
Well, that doesn't actually prove anything. *What it means is that Java
defined it that way (probably because it was easier to implement) and
the programmers accepted it. *It doesn't mean it is desirable (nor, of
course, does it mean it is undesirable).
You actually think anything in Java is defined the way it is defined
because "it was easier to implement"? Seriously?
On Fri, 28 Dec 2007 20:17:30 +0000, Flash Gordon
<sp**@flash-gordon.me.ukwro te:
>I hope I have corrected your misconceptions.
Good evening, Flash.
Respectfully, there were no misconceptions. There were only partial
answers. 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.
On Fri, 28 Dec 2007 12:23:01 -0800, Keith Thompson <ks***@mib.or g>
wrote:
>So, if i started off as, say, 2, then a[i] might be a[2], or it might be a[3].
That's only part of the problem. The behavior is completely undefined; a[i] might be a[42], or your left earlobe.
Good evening, Keith.
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.
Wrong?
Rick said:
<snip>
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.
Wrong?
Er, yeah, wrong. C doesn't actually guarantee this at all. But
realistically, how could it have any other value? Well, I don't plan to
work an example for you, but I recommend the following page, which gives
some hard data on the various results you get from different compilers for
similar expressions: http://www.phaedsys.demon.co.uk/chri...wengtips3a.htm
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
In article <dg************ *************** *****@4ax.com>,
Rick <re************ ****@nospam.now rote:
>On Fri, 28 Dec 2007 12:23:01 -0800, Keith Thompson <ks***@mib.or g> wrote:
>>So, if i started off as, say, 2, then a[i] might be a[2], or it might be a[3].
That's only part of the problem. The behavior is completely undefined; a[i] might be a[42], or your left earlobe.
Good evening, Keith.
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.
Wrong?
Wrong, by the standards of this newsgroup.
Here, what actually happens in the real world is irrelevant. In fact,
the real world itself is pretty much irrelevant. What matters is what
the standard requires, and the possible existence of a machine which has
read and understands the standard as well as the language lawyers
(aka, "the regulars") here have done.
So, the theory is that once you invoke "undefined behavior", anything
can happen (and does on the hypothetical machine described above),
including assigning a value to a[42] or starting global thermonuclear
war.
Rick wrote, On 28/12/07 23:25:
On Fri, 28 Dec 2007 20:17:30 +0000, Flash Gordon
<sp**@flash-gordon.me.ukwro te:
>>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
>>>>"R" == Rick <re************ ****@nospam.now rites:
RGood evening, Keith.
RIf...
Ri = 2; a[ i ] = i++;
R... then I claim that a[ i ] will be either a[ 2 ] or a[ 3 ],
Rdepending on whether i++ gets evaluated first or last, but it
Rmust be either one or the other.
RWrong?
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 cw*****@chromat ico.net
Charlton Wilbur <cw*****@chroma tico.netwrites:
>>>>>"R" == Rick <re************ ****@nospam.now rites:
RGood evening, Keith.
RIf...
Ri = 2; a[ i ] = i++;
R... then I claim that a[ i ] will be either a[ 2 ] or a[ 3 ],
Rdepending on whether i++ gets evaluated first or last, but it
Rmust be either one or the other.
RWrong?
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_Keit h) <ks***@mib.or g>
[...]
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister" This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: David Opstad |
last post by:
I have a question about text rendering I'm hoping someone here can
answer. Is there a way of doing linguistically correct rendering of
Unicode strings in Python? In simple cases like Latin or Japanese I can
just print the string and see the correct results. However, I don't know
how to get Python to do the right thing for writing systems which
require contextual processing.
For example, let's say I create this Unicode string in Arabic:
...
|
by: Sarah Tegtmeier |
last post by:
Hi
I have a question about the correct use of the attribute
xsi:schemaLocation. My programm has to process XML files where the value of
this attribute causes some problems. The programm is written in C++
using Xerces C++ version 2.3.0. An older older version of the programm
used Xerces C++ version 1.6.0.
The XML files look like the following example:
|
by: Richard Golebiowski |
last post by:
I have been trying to figure this out for quite some time and cannot
find any examples in VB.Net or in VB that work correctly. I am working
on an application where I want the user to be able to select a peinter
and printer tray to print reports out. In Crystal Reports 8.5, I can
select a printer and tray and it prints correctly.
I did a test report that I use to tell me the value that Crystal
Reports is using for the paper source. When I...
|
by: john.burton.email |
last post by:
I've done some extensive searching and can't seem to find an answer to
this -
Is it correct to using "using" with templates, for example:
using std::vector;
Or do I need to specify the type too:
using std::vector<int>;
Both seem to "work" on the compiler I have and I can't find any
documentation saying which is correct, or are both correct?
|
by: Rob Thorpe |
last post by:
Given the code:-
r = sscanf (s, "%lf", x);
What is the correct output if the string s is simply "-" ?
If "-" is considered the beginning of a number, that has been
cut-short then the correct output is that r = EOF. If it is taken to
be a letter in the stream, then the output should be r = 0, as far as
I can see. My compiler gives EOF.
| |
by: blackg |
last post by:
Input string not in correct format
--------------------------------------------------------------------------------
I am trying to view a picture from a table. I am getting this error
Input string not in the correct format.
Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it...
|
by: thisis |
last post by:
Hi All,
I need the PUBS.mdb for pulling images:
PUBS.mdb must have the table: pub_info
tbl_pub_info : has 3 fields
Data_Type : ok
Data_Type : ok
|
by: sehguh |
last post by:
Hiya Folks,
I am Currently using windows xp. Also using Visual Web Developer 2005 and Microsoft Sql server 2005.
The main page consists of an aspx page and a master page.
The page also consists of a label control(hidden when run in browser).
Also an Sql data source control connected to database tables for a photo album.
Also label web control ID=UserIdValue.
Also a Details View control ID=dvPictureInsert
Problem is how to work out...
|
by: lee123 |
last post by:
I have a problem getting the correct to count +1 every time I get an answer right
and the incorrect is the same.
I have two lbl's named number1 and number2 which produces a Rnd# in each lbl.
I have a txt box for the answer called useranswer.
A button to check if the answer is right called btnanswer.
I have just added two more lbl's for the correct named lblcorrect & the other is lblincorrect. How do I get this to count when the...
|
by: onetruelove |
last post by:
I want to creat a post like this blog:
http://onlinetoefltest.blogspot.com/2007/08/level-c-lesson-1.html
When you chose all the answers and click show answer a msg box will appear and tells how many answers are correct
I view the blog source and copied all the code to my post but it didn't work when i click the show answer button in my post.
Can any one help me with the code?
Thanks in advance
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |