473,406 Members | 2,343 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,406 software developers and data experts.

Undefined behaviour when modifying the result of an assignment operator

The C99 standard contains various statements like this one (in this
case, 6.5.16, assignment operator):
If an attempt is made to modify
the result of an assignment operator or to access it after the next sequence point, the
behavior is undefined.


What does this actually mean? Can anyone give me a code example that
leads to undefined behaviour?

Cheers

AL
Nov 14 '05 #1
7 1946
In article <m2********************************@4ax.com>,
Andy Lomax <ab***@[127.0.0.1]> wrote:
The C99 standard contains various statements like this one (in this
case, 6.5.16, assignment operator):
If an attempt is made to modify the result of an assignment operator or
to access it after the next sequence point, the behavior is undefined.


What does this actually mean? Can anyone give me a code example that leads
to undefined behaviour?


I would assume something like: (a=b)=c;

Nov 14 '05 #2
On Mon, 20 Jun 2005 15:21:47 GMT, ga*****@yin.interaccess.com (Kenny
McCormack) wrote:
In article <m2********************************@4ax.com>,
Andy Lomax <ab***@[127.0.0.1]> wrote:
The C99 standard contains various statements like this one (in this
case, 6.5.16, assignment operator):
If an attempt is made to modify the result of an assignment operator or
to access it after the next sequence point, the behavior is undefined.


What does this actually mean? Can anyone give me a code example that leads
to undefined behaviour?


I would assume something like: (a=b)=c;


I don't think this is what they mean - this would be a semantic error
(since the result of (a=b) isn't an lvalue), so the second assignment
is invalid, rather than having undefined behaviour.

It beats me how you can attempt to modify something which isn't an
lvalue without it being an error.

Cheers

AL

Nov 14 '05 #3
Andy Lomax <ab***@[127.0.0.1]> wrote [quoting the C standard]:
If an attempt is made to modify
the result of an assignment operator or to access it after the next sequence point, the
behavior is undefined.


What does this actually mean? Can anyone give me a code example that
leads to undefined behaviour?


struct { int a[2]; } s, t;
int *p;

(s = t).a[0] = 3; // attempting to modify the result of an assignment op.
p = (s = t).a;
p[0]; // attempting to access the result after the next s.p.

-Larry Jones

Hmph. -- Calvin
Nov 14 '05 #4
On Mon, 20 Jun 2005 16:13:01 GMT, la************@ugs.com wrote:
Andy Lomax <ab***@[127.0.0.1]> wrote [quoting the C standard]:
If an attempt is made to modify
the result of an assignment operator or to access it after the next sequence point, the
behavior is undefined.
What does this actually mean? Can anyone give me a code example that
leads to undefined behaviour?


struct { int a[2]; } s, t;
int *p;

(s = t).a[0] = 3; // attempting to modify the result of an assignment op.
p = (s = t).a;
p[0]; // attempting to access the result after the next s.p.

-Larry Jones

Hmph. -- Calvin


Very interesting - gcc/C90 gives a syntax error ('invalid use of
non-lvalue array for 'p=(s=t).a'), but gcc/C99 compiles Ok, but I
don't understand why.

The spec says:
An
assignment expression has the value of the left operand after the assignment, but is not an
lvalue.


So how is it possible to write '(s=t).a[0] = 3'?

Secondly, surely there's no problem accessing the result of an
assignment op *after* the next sequence point. Wouldn't the problem
arise if it was accessed *before* the next sequence point, which is
potentially before the update side-effect has taken place?

Cheers

AL
Nov 14 '05 #5
Andy Lomax <ab***@[127.0.0.1]> writes:
On Mon, 20 Jun 2005 16:13:01 GMT, la************@ugs.com wrote:
Andy Lomax <ab***@[127.0.0.1]> wrote [quoting the C standard]:

If an attempt is made to modify the result of an assignment
operator or to access it after the next sequence point, the
behavior is undefined.

What does this actually mean? Can anyone give me a code example that
leads to undefined behaviour?
struct { int a[2]; } s, t;
int *p;

(s = t).a[0] = 3; // attempting to modify the result of an assignment op.
p = (s = t).a;
p[0]; // attempting to access the result after the next s.p.


Very interesting - gcc/C90 gives a syntax error ('invalid use of
non-lvalue array for 'p=(s=t).a'), but gcc/C99 compiles Ok, but I
don't understand why.

The spec says:
An assignment expression has the value of the left operand after the
assignment, but is not an lvalue.


So how is it possible to write '(s=t).a[0] = 3'?


The result of the assignment expression is not an lvalue, but ...

The expression (s=t) yields a value (not an lvalue) of the above
struct type.

(s=t).a is of type int[2], which is implicitly converted to int*.
(This issue wouldn't occur if it were of type const int*, but the
language doesn't say there's a const, so there isn't.

(s=t).a[0] is therefore an lvalue of type int. There's no problem if
you want to read this value, but attempting to modify it invokes
undefined behavior.
Secondly, surely there's no problem accessing the result of an
assignment op *after* the next sequence point. Wouldn't the problem
arise if it was accessed *before* the next sequence point, which is
potentially before the update side-effect has taken place?


A struct assignment copies a struct value to a struct object and
yields the assigned value. Imagine that this value is stored in some
temporary location that no longer exists after the assignment (or
after the next sequence point). The assignment
p = (s = t).a;
grabs and saves the address of something within that temporary
storage. After the next sequence point, the storage should be assumed
to be gone, so any attempt to examine it invokes undefined behavior.

If you attempt to do something similar before the next sequence point,
either it's ok (because the temporary is still there), or it invokes
undefined behavior because of the other existing rules.

Note that something like this:

x = y;
x;

does not access the result of the assignment; it access the value of
x, which was set by an assignment that was completed before the
previous sequence point. The "result of the assignment" is a
temporary value that in most cases cannot be accessed as an object.
The obscure case above involving assignments of structures containing
arrays is, as far as I know, the only kind of thing that can cause
this problem.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #6
In article <m1********************************@4ax.com>,
Andy Lomax <ab***@[127.0.0.1]> wrote:
On Mon, 20 Jun 2005 15:21:47 GMT, ga*****@yin.interaccess.com (Kenny
McCormack) wrote:
In article <m2********************************@4ax.com>,
Andy Lomax <ab***@[127.0.0.1]> wrote:
The C99 standard contains various statements like this one (in this
case, 6.5.16, assignment operator):

If an attempt is made to modify the result of an assignment operator or
to access it after the next sequence point, the behavior is undefined.

What does this actually mean? Can anyone give me a code example that leads
to undefined behaviour?


I would assume something like: (a=b)=c;


I don't think this is what they mean - this would be a semantic error
(since the result of (a=b) isn't an lvalue), so the second assignment
is invalid, rather than having undefined behaviour.

It beats me how you can attempt to modify something which isn't an
lvalue without it being an error.


Yes, it is difficult. This is untested, and you may have to change it,
but you'll get the idea.

typedef struct { int x; int a [3]; } mystruct;

mystruct x, y;

(x = y).a [2] = 0;

The result of an assignment operator is not an lvalue. However, if it is
a struct, then you can access the struct members. And if one struct
member is an array, you can access the array elements and that gives you
an lvalue. And _then_ you can attempt to modify that lvalue.

So why is this undefined behavior? I guess that first of all compiler
writers were complaining: "Am I supposed to generate code for such
braindamaged garbage code?" I think your average compiler writer would
rather poke anyone writing that kind of code with pointy sticks until
they change their code, rather than producing code for it.

Second, the result of an assignment is not the left hand side - it is a
value that is equal to the left hand side. So what is supposed to be
changed by the "= 0" assignment? That's why it is undefined behavior.
Nov 14 '05 #7
On Mon, 20 Jun 2005 23:55:14 GMT, Keith Thompson <ks***@mib.org>
wrote:
The result of the assignment expression is not an lvalue, but ...

The expression (s=t) yields a value (not an lvalue) of the above
struct type.

(s=t).a is of type int[2], which is implicitly converted to int*.
(This issue wouldn't occur if it were of type const int*, but the
language doesn't say there's a const, so there isn't.

(s=t).a[0] is therefore an lvalue of type int. There's no problem if
you want to read this value, but attempting to modify it invokes
undefined behavior.


Thanks guys. It hadn't occurred to me that a value might contain an
lvalue.

AL
Nov 15 '05 #8

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

Similar topics

1
by: Dan Cernat | last post by:
Someone posted earlier an example that looked like taken from the standard. It was: i = 7, i++, i++; and the result should be 9 (i = 9 in the end) I thought that the compiler isn't required to do...
6
by: Simon Bailey | last post by:
In the following code at the end of the program z = 20 & y = 99. void doit(const int* x) { int* nonconst; nonconst = const_cast<int*>(x); *nonconst = 99; } int main(int argc, char* argv)
4
by: Mark Stijnman | last post by:
A while ago I posted a question about how to get operator behave differently for reading and writing. I basically wanted to make a vector that can be queried about whether it is modified recently...
7
by: duckfreezone | last post by:
Hi, I've got a small test program which behaves correctly (IMHO) on all compilers that I have acccess to, with the exception of the gcc on Macintosh OSX "gcc version 4.0.0 (Apple Computer, Inc....
14
by: ozbear | last post by:
Someone was asking in another forum how to translate: if (x && (y1=(y+y1)/2)) /* all integers with defined values */ to another language. I am at odds with myself as to whether this causes...
49
by: matty | last post by:
Hi, I recently got very confused (well that's my life) about the "undefined" value. I looked in the FAQ and didn't see anything about it. On...
45
by: VK | last post by:
(see the post by ASM in the original thread; can be seen at <http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/3716384d8bfa1b0b> as an option) As that is not in relevance to...
22
by: blargg | last post by:
Does ~0 yield undefined behavior? C++03 section 5 paragraph 5 seems to suggest so: The description of unary ~ (C++03 section 5.3.1 paragraph 8): But perhaps "one's complement" means the...
33
by: coolguyaroundyou | last post by:
Will the following statement invoke undefined behavior : a^=b,b^=a,a^=b ; given that a and b are of int-type ?? Be cautious, I have not written a^=b^=a^=b ; which, of course, is undefined....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.