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

Undefined behaviour? Accessing s twice in a statement.

Hello,

Wonder if the below double access to s is undefined behaviour? Works
fine, but I know "works" can still be a consequence of undefined
behaviour.

#include <string.h>

void mystrcat(char *s, const char *t)
{
/* is the below undefined behaviour ? */
s += strlen(s);

while(*s++ = *t++)
; /* empty statement */
}

S is accessed twice (which is a no no), yet the statement "feels" right
to me.

Nov 14 '05 #1
3 1157
In article <11**********************@f14g2000cwb.googlegroups .com>,
Kobu <ko********@gmail.com> wrote:
:Wonder if the below double access to s is undefined behaviour?

: s += strlen(s);

That is well defined. It is equivilent to s = s + strlen(s)
and you can see that on the right-hand side there are only
accesses to s without modifications. The restrictions
are against modifying the same value twice within the same
sequence point.

Consider that you would undoubtedly consider this valid:
y = 3 * x * x + x * 7 + 2;
(with x being a numeric variable): you don't worry about
pure accesses on the right hand side.

Similarily, you know that this is valid:
i = i + 1;
(with x being a numeric variable or a pointer): we'd have
some serious problems with programs if that didn't work.

So you can access the same variable twice on the RHS and
you can make an assignment on the LHS to a variable which
is mentioned on the RHS. Together, we can see that
s += strlen(s);
is valid.

What -would- be a problem is something like:

y = 3 * x * x++ + x++ * 7 + 2;

--
This signature intentionally left... Oh, darn!
Nov 14 '05 #2
Kobu wrote:
Wonder if the below double access to s is undefined behaviour? Works
fine, but I know "works" can still be a consequence of undefined
behaviour.

void mystrcat(char *s, const char *t)
{
/* is the below undefined behaviour ? */
s += strlen(s);

while(*s++ = *t++)
; /* empty statement */
}


It looks correct to me.

-Chris

Nov 14 '05 #3
In article <11**********************@f14g2000cwb.googlegroups .com>,
Kobu <ko********@gmail.com> wrote:
Hello,

Wonder if the below double access to s is undefined behaviour? Works
fine, but I know "works" can still be a consequence of undefined
behaviour.

#include <string.h>

void mystrcat(char *s, const char *t)
{
/* is the below undefined behaviour ? */
s += strlen(s);

while(*s++ = *t++)
; /* empty statement */
}

S is accessed twice (which is a no no), yet the statement "feels" right
to me.


Accessing s twice isn't a problem, if none of those accesses modifies it
(consider doing "i+i") or of only one of the accesses modifies it and
all the others are required to determine the new value to be stored
(as in your code).

The restrictions on multiple access are when an attempt is made to modify
an object twice between sequence points (i=i++) or when an object is
modified and an unrelated access to it is made (which is hard to come
up with a simple example for; aliasing and multiple side-effects are
usually involved - consider a[++*i]=a[++*j], if i or j point at the
element of a[] that gets modified).
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
Being rude and burbly in italian gets you killfiled just as fast as in
english. Some of us are multilingual.
--Mark McIntyre in comp.lang.c
Nov 14 '05 #4

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

Similar topics

48
by: marbac | last post by:
Hi, i heard a lot about "undefined behaviour" in this and other newsgroups dealing with c/c++. Is there a list where all cases with undefined behaviour in C++ are listed? regards marbac
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...
25
by: Nitin Bhardwaj | last post by:
Well, i'm a relatively new into C( strictly speaking : well i'm a student and have been doing & studying C programming for the last 4 years).....and also a regular reader of "comp.lang.c" I...
2
by: Deniz Bahar | last post by:
Hi, I'm working with a single linked list and want to delete elements by searching through the list (starting form the HEAD) then finding the element, then doing the following: NewElement =...
30
by: jimjim | last post by:
Hello, #include <stdio.h> int main(int argc, char *argv) { int x = 1; printf("%d %d %d\n", ++x, x, x++); return 0; }
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...
26
by: Frederick Gotham | last post by:
I have a general idea of the different kinds of behaviour described by the C Standard, such as: (1) Well-defined behaviour: int a = 2, b = 3; int c = a + b; (Jist: The code will work...
6
by: subramanian100in | last post by:
Suppose int i = 2; i = ++i + 1; Please explain whether the evaluation of this statement, has undefined behaviour ?
8
by: arnuld | last post by:
my friend sent me this programme: #include<iostream> int main() { int a=5; std ::cout << ++a << "\t" << ++a <<
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:
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
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,...
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
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,...

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.