473,698 Members | 2,521 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1965
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.int eraccess.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_Keit h) 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.int eraccess.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.or g>
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
2226
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 the assignement until the right hand side expression is fully evaluated. Apparently, the above expression is handled as: i = 7; i++; i++; which I don't think is right. Could someone explain, please?
6
2143
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
2080
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 or not. My first idea, using the const and non-const versions of operator, was clearly not correct, as was pointed out. Julián Albo suggested I could use proxies to do that. I've done some googling for proxies (also in this group) and personally,...
7
1512
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. build 5026)" Looks to be that the order of evaluation of the assignment on OSX is different to other systems. I *think* it is wrong on OSX, or am I simply relying on undefined behaviour?
14
1740
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 undefined behaviour in the first place. Does the subexpression (y1=(y+y1)/2) cause UB because /y/ is both modified and used without an intervening sequence point attached to the larger expression or is it ok because the value of the assignment is...
49
14499
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 http://www.webreference.com/programming/javascript/gr/column9/ they say: <snip> The undefined property A relatively recent addition to JavaScript is the undefined property.
45
4843
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 "new Array() vs " question or to the array performance, I dared to move it to a new thread. Gecko takes undefined value strictly as per Book 4, Chapter 3, Song 9 of Books of ECMA :-)
22
2015
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 value that type would have with all bits inverted, rather than the mathematical result of inverting all bits in the binary representation. For example, on a machine with 32-bit
33
2827
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. I am having some confusion with the former statement! Also, state the reason for the statement being undefined!
0
8683
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
8611
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,...
0
9170
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7741
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6531
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
4372
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...
2
2341
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.