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

tricky assignment statemenent

p = p->next = q;

Why it is undefined?
operator "=" is right-associative i.e. "p = p->next = q;" is
equivalent to "p = (p->next = q);
And I think another interpretation of sequence point is: between 2
sequence point a variable cannot be modified twice. In "p = p->next =
q;", "p->next" is modified once, "p" is modified once. (Let say we use
another pointer "k" such that "k=p->next" and then "p = p->next = q;"
become "p = k = q". Obviously the "k" and "p" are modified once
individually.)
Nov 14 '05 #1
6 1909
In article <d0**************************@posting.google.com >,
cc****@hotmail.com (ccwork) wrote:
p = p->next = q;

Why it is undefined?
operator "=" is right-associative i.e. "p = p->next = q;" is
equivalent to "p = (p->next = q);
And I think another interpretation of sequence point is: between 2
sequence point a variable cannot be modified twice. In "p = p->next =
q;", "p->next" is modified once, "p" is modified once. (Let say we use
another pointer "k" such that "k=p->next" and then "p = p->next = q;"
become "p = k = q". Obviously the "k" and "p" are modified once
individually.)


There are no rules in C in which order operands are evaluated.

The value of q will eventually be stored into p. Will the p in "p->next"
be read before or after q is stored into p? If you find where in the C
Standard this is defined, then fine. Good luck looking for it.
Nov 14 '05 #2
Christian Bau wrote:
In article <d0**************************@posting.google.com >,
cc****@hotmail.com (ccwork) wrote:
p = p->next = q;

Why it is undefined?
operator "=" is right-associative i.e. "p = p->next = q;" is
equivalent to "p = (p->next = q);
And I think another interpretation of sequence point is: between 2
sequence point a variable cannot be modified twice. In "p = p->next = q;", "p->next" is modified once, "p" is modified once. (Let say we use another pointer "k" such that "k=p->next" and then "p = p->next = q;" become "p = k = q". Obviously the "k" and "p" are modified once
individually.)
There are no rules in C in which order operands are evaluated.

The value of q will eventually be stored into p. Will the p in

"p->next" be read before or after q is stored into p?


Before. In some cases there are implicit rules about the relative
order of evaluation of expressions. Let's take the trivial example of:

p = p + 1;

According to your logic, this would be undefined. However, 6.5.2 gets
us off the hook:

6.5 Expressions
..
.. 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 accessed only to determine the value to be stored.

The expression 'p + 1' represents 'the value to be stored'. Within 'p
+ 1', p represents 'the prior value'. Here, p is 'accessed only to
determine the value to be stored'.

Simply mentioning 'the prior value' creates an implicit, partial
operand evaluation order. p must not be modified until its 'prior
value' is evaluated.

The expression:

p = p->next = q;

Is evaluated much the same as the above, and is defined behavior.
'p->next = q' is the 'value to be stored'. Since p is being modified
in the expression, the p in p->next is the 'prior value'. Since
p->next is a participant in the expression 'p->next = q' (the 'value to
be stored'), it's clear that 'the prior value [is] accessed only to
determine the value to be stored'.

It *is* defined behavior. Whether or not it is good style is another
matter altogether.
Mark F. Haigh
mf*****@sbcglobal.net

Nov 14 '05 #3
In article <11**********************@f14g2000cwb.googlegroups .com>,
mf*****@sbcglobal.net wrote:
The expression:

p = p->next = q;

Is evaluated much the same as the above, and is defined behavior.
'p->next = q' is the 'value to be stored'. Since p is being modified
in the expression, the p in p->next is the 'prior value'. Since
p->next is a participant in the expression 'p->next = q' (the 'value to
be stored'), it's clear that 'the prior value [is] accessed only to
determine the value to be stored'.
The prior value is most definitely _not_ only accessed to determine the
value to be stored. It is _also_ accessed to determine the address of
another memory location (&p->next) whose contents is also modified.
There are two assignments here, two memory locations that are modified.
How would you determine the second location without reading p?

It *is* defined behavior. Whether or not it is good style is another
matter altogether.


Absolutely not defined. Similar to "a [i] = i++; ": i is modified. The
prior value is accessed to determine the new value stored in i. i is
also accessed for another purpose: to determine a memory location where
the value of i++ will be stored. Undefined behavior.
Nov 14 '05 #4
On Fri, 07 Jan 2005 21:52:11 +0000, Christian Bau wrote:
In article <d0**************************@posting.google.com >,
cc****@hotmail.com (ccwork) wrote:
p = p->next = q;

Why it is undefined?
operator "=" is right-associative i.e. "p = p->next = q;" is
equivalent to "p = (p->next = q);
And I think another interpretation of sequence point is: between 2
sequence point a variable cannot be modified twice. In "p = p->next =
q;", "p->next" is modified once, "p" is modified once. (Let say we use
another pointer "k" such that "k=p->next" and then "p = p->next = q;"
become "p = k = q". Obviously the "k" and "p" are modified once
individually.)
There are no rules in C in which order operands are evaluated.


There are for operators like && and ? :

However the issue here is not in what order operands are evaluated it is
about whether the operands have to be evaluated before the operation is
performed and a result produced. It is an issue of sequencing based on
data dependency.
The value of q will eventually be stored into p. Will the p in "p->next"
be read before or after q is stored into p? If you find where in the C
Standard this is defined, then fine. Good luck looking for it.


I've already explained this. :-)

Lawrence
Nov 14 '05 #5
Christian Bau wrote:
In article <11**********************@f14g2000cwb.googlegroups .com>,
mf*****@sbcglobal.net wrote:
The expression:

p = p->next = q;

Is evaluated much the same as the above, and is defined behavior.
'p->next = q' is the 'value to be stored'. Since p is being
modified in the expression, the p in p->next is the 'prior value'.
Since p->next is a participant in the expression 'p->next = q'
(the 'value to be stored'), it's clear that 'the prior value [is]
accessed only to determine the value to be stored'.


The prior value is most definitely _not_ only accessed to determine
the value to be stored. It is _also_ accessed to determine the
address ofanother memory location (&p->next) whose contents is
also modified. There are two assignments here, two memory
locations that are modified. How would you determine the
second location without reading p?


Yes, it is defined behavior. Consider

int i = 5;
/* ..... */
i = i*0;

Would you argue that this has undefined behavior?

It *is* defined behavior. Whether or not it is good style is
another matter altogether.


Absolutely not defined. Similar to "a [i] = i++; ": i is modified.
The prior value is accessed to determine the new value stored in i.
i is also accessed for another purpose: to determine a memory
location where the value of i++ will be stored. Undefined behavior.


Here, i is both modified in the right-hand side and accessed in the
left-hand side. Since the access is not used to determine the stored
value (as it does not occur in the modifying expression) this is indeed
undefined behavior.

Nov 14 '05 #6

"Eric Schmidt" <er*******@comcast.net> wrote in message news:11**********************@z14g2000cwz.googlegr oups.com...
Christian Bau wrote:

[...]

The prior value is most definitely _not_ only accessed to determine
the value to be stored. It is _also_ accessed to determine the
address ofanother memory location (&p->next) whose contents is
also modified. There are two assignments here, two memory
locations that are modified. How would you determine the
second location without reading p?


Yes, it is defined behavior. Consider

int i = 5;
/* ..... */
i = i*0;

Would you argue that this has undefined behavior?


In your example, "i" is accessed to determine the new value to be
stored into "i" and it's the *only* purpose for the access, so it's
defined. Which is quite different from the OP's tricky example. If you
are claiming that because "i" is multiplied by 0 one can determine
the value to be stored with no access to "i" and so the access to "i"
on the right-hand side is not to determine the to-be-stored value,
then what you think is completely irrelevant to the standard; it's
about optimization. According to your logic (assumed above), do you
really think that the following is well defined BY the standard?

int *p; /* not initialized */
*p = *p * 0;
--
Jun, Woong (woong at icu.ac.kr)
Information and Communications Univ.

Nov 14 '05 #7

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

Similar topics

23
by: Paul Rubin | last post by:
OK, I want to scan a file for lines matching a certain regexp. I'd like to use an assignment expression, like for line in file: if (g := re.match(pat, line)): croggle(g.group(1)) Since...
25
by: PyPK | last post by:
What possible tricky areas/questions could be asked in Python based Technical Interviews?
7
by: David Poundall | last post by:
importedfiles = {} for f in FileList f2 = f.split('.') # strip the .py, .pyc __import__(f2) s2 = f2+'.main()' # main is the top file in each import c = compile(s2, '', 'eval')...
3
by: rittersporn | last post by:
I want to model relations between "Groups" and also annotate the relations! My solution so far: Group - GroupName : Text | PRIMARY_KEY - Titel : Text - Info : Memo
3
by: ccwork | last post by:
>There are two assignments here. In one assignment, p is modified. The >prior value of p is arguably used to determine the new value stored into >p, as you explained, but it is _also_ read to...
15
by: fungus | last post by:
I'm moving some code from VC++ 6 to VC++ 2005 and I've run into a nasty problem because iterators are no longer pointers. In the program I'm moving, there's a std::vector of items hidden inside...
11
by: onkar | last post by:
Program 1: #include<stdio.h> int main(void){ int *p; p=(int *)malloc(sizeof(int)); *p=12; printf("%d %p\n",*p,p); return 0; }
3
by: =?Utf-8?B?dGhlamFja29mYWxs?= | last post by:
I have a few global variables and a function that gets called multiple times to complete a single transaction and uses the variables. The function gets different notifications. When the function...
9
by: raylopez99 | last post by:
Just an observation: pens for drawing lines in Win Forms are tricky when assignment is inside the paint handler. inside of the Paint handler, but not inside a "using" brace (that is, outside of...
3
by: Joseph Geretz | last post by:
I'm using the Request Filter documentation which can be found here: http://msdn.microsoft.com/en-us/library/system.web.httprequest.filter.aspx In this example, two filters are installed, one...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
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,...

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.