Re: tricky assignment statemenent
From: Christian Bau (christian.bau_at_cbau.freeserve.co.uk)
Date: 01/04/05
- Next message: petermcmillan_uk_at_yahoo.com: "Re: tricky assignment statemenent"
- Previous message: Michael Wojcik: "Re: Language efficiency of C versus FORTRAN et al"
- In reply to: Lawrence Kirby: "Re: tricky assignment statemenent"
- Next in thread: Tim Rentsch: "Re: tricky assignment statemenent"
- Reply: Tim Rentsch: "Re: tricky assignment statemenent"
- Reply: Lawrence Kirby: "Re: tricky assignment statemenent"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 04 Jan 2005 18:07:59 +0000
In article <pan.2005.01.04.16.10.22.281000@netactive.co.uk>,
Lawrence Kirby <lknews@netactive.co.uk> wrote:
> On Fri, 31 Dec 2004 05:50:38 +0000, Joona I Palaste wrote:
>
> > Ike Naar <nospam@nospam.invalid> scribbled the following:
> >> Consider the following code:
>
> ...
>
> >> void f(struct s * q)
> >> {
> >> struct s * p = malloc(sizeof *p);
> >> if (p != 0)
> >> {
> >> p = p->next = q; /* is this safe? */
> >> }
> >> }
> >
> >> In the context given above, is the statement "p = p->next = q;"
> >> safe and equivalent to "p->next = q; p = q;"
> >> or does it invoke undefined behaviour because p is modified and
> >> dereferenced at the same time?
> >
> > I would consider it undefined behaviour, as there is no sequence point
> > between the operations "p=p->next" and "p->next=q" in the code.
>
> However the standard says "Furthermore, the prior value shall be read only
> to determine the value to be stored." In p = p->next = q the value
> assigned to p is the result of the expression p->next = q. In the abstract
> machine this expression must be evaluated fully in order to generate the
> value to be assigned. The access to p in p->next is part of that
> evaluation so it is very much being read in order to determine the new
> value. Before you start formulting your objections to this consider
> carefully the statements in the standard (5.1.2.3):
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.
- Next message: petermcmillan_uk_at_yahoo.com: "Re: tricky assignment statemenent"
- Previous message: Michael Wojcik: "Re: Language efficiency of C versus FORTRAN et al"
- In reply to: Lawrence Kirby: "Re: tricky assignment statemenent"
- Next in thread: Tim Rentsch: "Re: tricky assignment statemenent"
- Reply: Tim Rentsch: "Re: tricky assignment statemenent"
- Reply: Lawrence Kirby: "Re: tricky assignment statemenent"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|