473,770 Members | 6,978 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1948
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*****@sbcglob al.net

Nov 14 '05 #3
In article <11************ **********@f14g 2000cwb.googleg roups.com>,
mf*****@sbcglob al.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************ **********@f14g 2000cwb.googleg roups.com>,
mf*****@sbcglob al.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*******@comc ast.net> wrote in message news:11******** **************@ z14g2000cwz.goo glegroups.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
3234
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 there are no assignment expressions in Python, I have to use a temp var. That's a little more messy, but bearable:
25
3408
by: PyPK | last post by:
What possible tricky areas/questions could be asked in Python based Technical Interviews?
7
1781
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') importedfiles = eval(c) 'importedfiles' should hold an object reference to the main() function within each imported file.
3
1348
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
311
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 determine at which memory >location the second assignment will happen, so it is not _only_ read to >determine the value stored, therefore undefined behavior. There is something wrong. The first assignment is "p->next = q" and
15
2185
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 a class and users of the class get to iterate it via a const_iterator. eg.
11
2403
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
1647
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 receives a final notification, that where I need to release the global variables. I don't have control over when the function gets called because the function is called by the system. How can I block the function so that it can only be entered...
9
2360
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 "using { Pen mypen = new Pen(Color.Black, 1)) {}), which I think makes a difference: I find the following assignment does not work: //myPenTest instantiated in the normal constructor, as was baseline,
3
1769
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 filter uppercases every alphabetic character and then the second filter replaces every 'E' with "#'. OK, very nice. What's trivial about these examples is that the returned string is exactly the same length as the string which is actually read...
0
9591
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
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,...
1
10002
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,...
0
9869
tracyyun
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...
1
7415
isladogs
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...
0
6676
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();...
0
5312
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...
1
3970
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3575
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.