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

Self-assignment -- undefined?


Does the following snippet exhibit undefined behaviour (excuse the stupidity
of the code):

int i = 5;

i = i;

Or what about a more complex form of it:

void Func(int *const p,int const *const q)
{
*p = *q;
}

int main(void)
{
int i = 5;

Func(&i,&i);

return 0;
}

I'm suspicious that maybe a sequence point is being violated.

--

Frederick Gotham
Nov 9 '06 #1
16 1679


Frederick Gotham wrote On 11/09/06 11:24,:
Does the following snippet exhibit undefined behaviour (excuse the stupidity
of the code):

int i = 5;

i = i;
No.
Or what about a more complex form of it:

void Func(int *const p,int const *const q)
{
*p = *q;
}

int main(void)
{
int i = 5;

Func(&i,&i);

return 0;
}
No.
I'm suspicious that maybe a sequence point is being violated.
There's no such thing as "violating a sequence point."
You're probably referring to the rules that require certain
operations -- two modifications to the same object, for
example -- to be separated by at least one sequence point,
but the code snippets above do not break those rules.

--
Er*********@sun.com

Nov 9 '06 #2
Hi Frederick,
Does the following snippet exhibit undefined behaviour (excuse the stupidity
of the code):

int i = 5;

i = i;

Or what about a more complex form of it:

void Func(int *const p,int const *const q)
{
*p = *q;
}

int main(void)
{
int i = 5;

Func(&i,&i);

return 0;
}

I'm suspicious that maybe a sequence point is being violated.
For those trivial examples, AFAIK no. Of course, things can become
tricky for instance if your Func assumes different addresses for the
arguments to work correctly.

Cheers,
Loic.

Nov 9 '06 #3
lo******@gmx.net wrote:
>
Hi Frederick,
Does the following snippet exhibit undefined behaviour (excuse the stupidity
of the code):

int i = 5;

i = i;

Or what about a more complex form of it:

void Func(int *const p,int const *const q)
{
*p = *q;
}

int main(void)
{
int i = 5;

Func(&i,&i);

return 0;
}

I'm suspicious that maybe a sequence point is being violated.

For those trivial examples, AFAIK no. Of course, things can become
tricky for instance if your Func assumes different addresses for the
arguments to work correctly.
For example, if Func() used:

*p = (*q)++;

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Nov 9 '06 #4
2006-11-09 <1163090289.653296@news1nwk>,
Eric Sosman wrote:
>

Frederick Gotham wrote On 11/09/06 11:24,:
>Does the following snippet exhibit undefined behaviour (excuse the stupidity
of the code):

int i = 5;

i = i;

No.
>Or what about a more complex form of it:

void Func(int *const p,int const *const q)
{
*p = *q;
}

int main(void)
{
int i = 5;

Func(&i,&i);

return 0;
}

No.
>I'm suspicious that maybe a sequence point is being violated.

There's no such thing as "violating a sequence point."
You're probably referring to the rules that require certain
operations -- two modifications to the same object, for
example
or a modification and an access IIRC
-- to be separated by at least one sequence point,
but the code snippets above do not break those rules.
Nov 9 '06 #5


Jordan Abel wrote On 11/09/06 13:14,:
2006-11-09 <1163090289.653296@news1nwk>,
Eric Sosman wrote:
[...]
> There's no such thing as "violating a sequence point."
You're probably referring to the rules that require certain
operations -- two modifications to the same object, for
example


or a modification and an access IIRC

>>-- to be separated by at least one sequence point,
but the code snippets above do not break those rules.
Depends on nature of the access. 6.5/2:

Between the previous and next sequence point an
object shall have its stored value modified at
most once by the evaluation of an expression.
Furthermore, the prior value shall be read only
to determine the value to be stored.

I've always found that second sentence troubling, because
it seems to ascribe "purpose" or "intent" to the workings
of the abstract machine. "Only to determine" is hard to
read -- for me, anyhow -- in terms of an implementation
rather than in terms of a programmer or implementor. The
programmer has purposes and does things for reasons, but
the implementation just exhibits the behavior the Standard
describes, without intent and without consiousness.

However, there's a footnote that says `i = ++i + 1' and
`a[i++] = i' are undefined while `i = i + 1' and `a[i] = i'
are allowed. The second example seems to be the one that
illustrates the second sentence: `i' is modified, and its
prior value is used in two different ways. As an operand
of `++', the prior value determines the new value (and the
sub-expression value), and this is allowed. The reference
on the r.h.s., though, is not "for the purpose of" computing
the new value, so it's disallowed. The non-normative footnote
conveys the meaning better (to me) than the normative text.

--
Er*********@sun.com

Nov 9 '06 #6

Frederick Gotham wrote:
Does the following snippet exhibit undefined behaviour ... [?]
[something like *p = *p; ]
The following is *very* OT and perhaps unresponsive
but may be of interest to those curious to hear
about strange machines doing strange things.

<true anecdote>

When the IBM 370 Model 135 or 138 did a memory-to-memory
move (MVC or MVCL opcode) with addresses and counts
all multiples of 8, there are several cases, most of which work
correctly.

However, in the case (which perhaps never happened under
IBM OS'es) that *p1 = *p2 is executed with p1,p2 different
*virtual* addresses which map to the same *real* addresses,
then the data in memory could change! Specifically, a
correctible SBE in the second half of a 8-byte doubleword
could be flipped, i.e. the SBE would be uncorrected and written
back (with check bits no longer reflecting the error.)

Reason: 370 architecture defined such a non-overlapping
MVC or MVCL as "validating". (In the example, real addresses
do overlap, but firmware doesn't detect this.) Things would
still be OK, *except* that, due to a paucity of work registers,
the 135/138 firmware was unable to do
read 8 bytes
write 8 bytes
and instead did
read 4 bytes
write 4 bytes
read 4 bytes
write 4 bytes
The "validation" flag disabled ECC on the first write.

</true anecdote>

*Totally* irrelevant I know, but such things seemed very *fun*
for me 30 years ago. Next time I'll tell you about a machine
in Athens, Georgia which failed a silly diagnostic for which
the simplest fix was to place a 100 pF capacitor on a system
clock!

James D. Allen

Nov 10 '06 #7
James Dow Allen wrote:
>
.... snip ...
>
*Totally* irrelevant I know, but such things seemed very *fun*
for me 30 years ago. Next time I'll tell you about a machine
in Athens, Georgia which failed a silly diagnostic for which
the simplest fix was to place a 100 pF capacitor on a system
clock!
Nothing unusual about that. Normally discovered by the fact that
the system works fine with a scope probe in place.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Nov 10 '06 #8
In article <11*********************@f16g2000cwb.googlegroups. com"James Dow Allen" <jd*********@yahoo.comwrites:
....
*Totally* irrelevant I know, but such things seemed very *fun*
for me 30 years ago. Next time I'll tell you about a machine
in Athens, Georgia which failed a silly diagnostic for which
the simplest fix was to place a 100 pF capacitor on a system
clock!
Let me tell you about that multi-million dollar machine that on occasion
would produce wrong result. The reason? A fan mounted the wrong way
around.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 10 '06 #9
James Dow Allen wrote:
[...]
*Totally* irrelevant I know, but such things seemed very *fun*
for me 30 years ago. Next time I'll tell you about a machine
in Athens, Georgia which failed a silly diagnostic for which
the simplest fix was to place a 100 pF capacitor on a system
clock!
I assume we've all read this entry from the Jargon file:

http://jargon.watson-net.com/section...gic-story.html

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Nov 10 '06 #10
"Dik T. Winter" <Di********@cwi.nlwrote:
In article <11*********************@f16g2000cwb.googlegroups. com"James Dow Allen" <jd*********@yahoo.comwrites:
...
*Totally* irrelevant I know, but such things seemed very *fun*
for me 30 years ago. Next time I'll tell you about a machine
in Athens, Georgia which failed a silly diagnostic for which
the simplest fix was to place a 100 pF capacitor on a system
clock!

Let me tell you about that multi-million dollar machine that on occasion
would produce wrong result. The reason? A fan mounted the wrong way
around.
If there are several ways to do something, and one of these ways leads
to disaster, then sooner or later someone _will_ do it that way.

Richard
Nov 13 '06 #11
Kenneth Brody <ke******@spamcop.netwrote:
James Dow Allen wrote:
[...]
*Totally* irrelevant I know, but such things seemed very *fun*
for me 30 years ago. Next time I'll tell you about a machine
in Athens, Georgia which failed a silly diagnostic for which
the simplest fix was to place a 100 pF capacitor on a system
clock!

I assume we've all read this entry from the Jargon file:

http://jargon.watson-net.com/section...gic-story.html
That's not TJF, that's a cheap, CSS-unhanced knock-off. And a slightly
old one, too. _This_ is TJF:

<http://www.catb.org/~esr/jargon/html/magic-story.html>

HTH; HAND.

Richard
Nov 13 '06 #12

Richard Bos wrote:
"Dik T. Winter" <Di********@cwi.nlwrote:
In article <11*********************@f16g2000cwb.googlegroups. com"James Dow Allen" <jd*********@yahoo.comwrites:
...
*Totally* irrelevant I know, but such things seemed very *fun*
for me 30 years ago. Next time I'll tell you about a machine
in Athens, Georgia which failed a silly diagnostic for which
the simplest fix was to place a 100 pF capacitor on a system
clock!
Let me tell you about that multi-million dollar machine that on occasion
would produce wrong result. The reason? A fan mounted the wrong way
around.

If there are several ways to do something, and one of these ways leads
to disaster, then sooner or later someone _will_ do it that way.
http://en.wikipedia.org/wiki/Genesis_(spacecraft)
>
Richard
Nov 13 '06 #13
Richard Bos wrote:
>
Kenneth Brody <ke******@spamcop.netwrote:
James Dow Allen wrote:
[...]
*Totally* irrelevant I know, but such things seemed very *fun*
for me 30 years ago. Next time I'll tell you about a machine
in Athens, Georgia which failed a silly diagnostic for which
the simplest fix was to place a 100 pF capacitor on a system
clock!
I assume we've all read this entry from the Jargon file:

http://jargon.watson-net.com/section...gic-story.html

That's not TJF, that's a cheap, CSS-unhanced knock-off. And a slightly
old one, too. _This_ is TJF:

<http://www.catb.org/~esr/jargon/html/magic-story.html>

HTH; HAND.
That's what happens when you're lazy and don't take the time to find
"The Real Jargon File"[tm] whereabouts. G may be YF, but it's no
substitute for using your brain. :-)

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>

Nov 13 '06 #14
In article <45****************@news.xs4all.nl>,
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
If there are several ways to do something, and one of these ways leads
to disaster, then sooner or later someone _will_ do it that way.
Sooner or later? It's Microsoft policy.

--
/~\ cg****@kltpzyxm.invalid (Charlie Gibbs)
\ / I'm really at ac.dekanfrus if you read it the right way.
X Top-posted messages will probably be ignored. See RFC1855.
/ \ HTML will DEFINITELY be ignored. Join the ASCII ribbon campaign!

Nov 17 '06 #15
Charlie Gibbs wrote:
In article <45****************@news.xs4all.nl>,
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
>If there are several ways to do something, and one of these ways leads
to disaster, then sooner or later someone _will_ do it that way.

Sooner or later? It's Microsoft policy.
So it will be delivered late.

--
Roland HutchinsonÂ*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*Â*WillÂ*playÂ* violaÂ*daÂ*gambaÂ*forÂ*food.

NB mail to my.spamtrap [at] verizon.net is heavily filtered to
remove spam.Â*Â*IfÂ*yourÂ*messageÂ*looksÂ*likeÂ*spamÂ*IÂ* mayÂ*notÂ*seeÂ*it.
Nov 18 '06 #16
In article <11*********************@e3g2000cwe.googlegroups.c om>,
toby <to**@telegraphics.com.auwrote:
>
Richard Bos wrote:
>"Dik T. Winter" <Di********@cwi.nlwrote:
>
Let me tell you about that multi-million dollar machine that on occasion
would produce wrong result. The reason? A fan mounted the wrong way
around.

If there are several ways to do something, and one of these ways leads
to disaster, then sooner or later someone _will_ do it that way.

http://en.wikipedia.org/wiki/Genesis_(spacecraft)
Somewhere in my wife's lab (and perhaps posted here before) is a sheet
of paper explaining certain phrases that may be found in scientific
papers. To paraphrase a couple:

Results are inconclusive (I dropped it) but some useful results were
found (I scooped up most of it).
--
Frankfort, Kentucky, makes it against the law to shoot off a
policeman's tie.
Nov 29 '06 #17

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

Similar topics

2
by: Marc | last post by:
Hi all, I was using Tkinter.IntVar() to store values from a large list of parts that I pulled from a list. This is the code to initialize the instances: def initVariables(self): self.e =...
15
by: Ralf W. Grosse-Kunstleve | last post by:
****************************************************************************** This posting is also available in HTML format: http://cci.lbl.gov/~rwgk/python/adopt_init_args_2005_07_02.html...
18
by: Ralf W. Grosse-Kunstleve | last post by:
My initial proposal (http://cci.lbl.gov/~rwgk/python/adopt_init_args_2005_07_02.html) didn't exactly get a warm welcome... And Now for Something Completely Different: class autoinit(object):...
4
by: David Coffin | last post by:
I'd like to subclass int to support list access, treating the integer as if it were a list of bits. Assigning bits to particular indices involves changing the value of the integer itself, but...
4
by: marek.rocki | last post by:
First of all, please don't flame me immediately. I did browse archives and didn't see any solution to my problem. Assume I want to add a method to an object at runtime. Yes, to an object, not a...
7
by: Andrew Robert | last post by:
Hi Everyone, I am having a problem with a class and hope you can help. When I try to use the class listed below, I get the statement that self is not defined. test=TriggerMessage(data) var...
24
by: Peter Maas | last post by:
The Python FAQ 1.4.5 gives 3 reasons for explicit self (condensed version): 1. Instance variables can be easily distinguished from local variables. 2. A method from a particular class can be...
84
by: braver | last post by:
Is there any trick to get rid of having to type the annoying, character-eating "self." prefix everywhere in a class? Sometimes I avoid OO just not to deal with its verbosity. In fact, I try to...
13
by: Kurda Yon | last post by:
Hi, I found one example which defines the addition of two vectors as a method of a class. It looks like that: class Vector: def __add__(self, other): data = for j in range(len(self.data)):...
6
by: Bart Kastermans | last post by:
I am playing with some trees. In one of the procedures I wrote for this I am trying to change self to a different tree. A tree here has four members (val/type/left/right). I found that self = SS...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.