473,902 Members | 5,194 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

*p++ = *q++ undefined? why?

hello,

i'm trying to understand what are the rules for determining when an
expression is well defined or not. i found a page
(http://c-faq.com/expr/seqpoints.html) which seems to explain this well
enough.

first of all some terms:
* object = variable, element of an array, location pointed by a pointer
* modification = assignment ( = += ... ), increment/decrement
(postfixed/prefixed)

if i understood correctly, there are two rules (checked in that order):
1) each object in an expression can be modified no more than once;
2) if an object is modified, then all the times that object is accessed
in order to be read, the values that are read must be all used for
calculating the final value of the object.

now give a look at the expression at http://c-faq.com/expr/confused.html
(end of point 3):

*p++ = *q++

i can't understand the following sentence:

"[...] in which three things are modified (p, q, and *p [...]), are
allowed if all three objects are distinct, i.e. only if two different
pointers p and q [...] are used."

i explain you my reasoning. first of all that expression could be
rewritten as:

((*(p++)) = (*(q++)))

there are 4 objects: p, q, *(p++), *(q++).

p, q and *(p++) are modified only once, so they follow rule 1.
*(q++) is not modified, so it also follows rule 1.

p is read only once, and the value of this reading is used for
calculating the final value of p, so rule 2 is respected. the same for
q. *(p++) is never read, so it also respects rule 2.

so, why that expression is undefined?

to be honest, it does not say it is "undefined" , but it speaks about
being allowed or not. so: in which case this expression is not allowed?
i can't see when...

also, the compiler (gcc) does not complain as in:

i = i++ /* warning: operation on `i' may be undefined */

so, what's the problem?
Apr 1 '06
57 7679

pete wrote:
Michael Mair wrote:

pete schrieb:
Vladimir S. Oka wrote:
>Eric Sosman opined:
>>CBFalconer wrote:
>>>pete wrote:
>>>
>>>... snip ...
>>>
>>>>As long as (*p) is defined and (*q) is defined,
>>>>there is nothing wrong with (*p++ = *q++).
>>>
>>>Assuming p != q.
>>
>> Even if p == q. (Confession: I very nearly made
>>the same mistake.)
>
>Now I can see it as well! Apologies for the confusion.
>
>Now that I actually thought it through, isn't the above the very
>construct K&R use to implement `strcpy()`? I left my copy at work, so
>I can't look it up, but I'm pretty sure. Along the lines of:
>
> while (*p++ = *q++)
> ;

Yes.
/* strcpy: copy t to s; pointer version 3 */
void strcpy(char *s, char *t)
{
while (*s++ = *t++)
;
}

You can see how that implementation of strcpy
would be undefined with something like:

char char_array[] = "xx\0";

strcpy(char_arr ay, char_array + 1);

though

memmove(char_ar ray, char_array + 1, 1 + strlen(char_arr ay + 1));

would be fine.


Why? How? I cannot see anything undefined in that.
If you wrote "strcpy(char_ar ray + 1, char_array);", then I would
agree.


That's what I meant write.


I must be going mad, or missing something here - are you saying that
the code below gives UB?

#include <stdio.h>

void strcpy(char *s, char *t)
{
while(*s++ = *t++)
;
}
int main(void)
{
char char_array[] = "xx\0";

strcpy(1 + char_array, char_array);

return 0;
}

If 'yes', can you explain why please.

while(*s++ = *t++)

As far as I can see, the assignment expression here will 'run' like
this:

1. eval *t

2. take value found in '1' - write to *s

3. increment s then t, OR, increment t then s.

4. repeat.

Apr 2 '06 #21
pemo wrote:
I must be going mad, or missing something here - are you saying that
the code below gives UB?

#include <stdio.h>

void strcpy(char *s, char *t)
{
while(*s++ = *t++)
;
}

int main(void)
{
char char_array[] = "xx\0";

strcpy(1 + char_array, char_array);

return 0;
}

If 'yes', can you explain why please.

while(*s++ = *t++)

As far as I can see, the assignment expression here will 'run' like
this:

1. eval *t

2. take value found in '1' - write to *s

3. increment s then t, OR, increment t then s.

4. repeat.


When does the 'run' stop?

--
pete
Apr 2 '06 #22
pemo wrote:
pete wrote:
Michael Mair wrote:
pete schrieb:
Vladimir S. Oka wrote:
> Eric Sosman opined:
>> CBFalconer wrote:
>>> pete wrote:
>>>
>>> ... snip ...
>>>
>>>> As long as (*p) is defined and (*q) is defined,
>>>> there is nothing wrong with (*p++ = *q++).
>>> Assuming p != q.
>> Even if p == q. (Confession: I very nearly made
>> the same mistake.)
> Now I can see it as well! Apologies for the confusion.
>
> Now that I actually thought it through, isn't the above the very
> construct K&R use to implement `strcpy()`? I left my copy at work, so
> I can't look it up, but I'm pretty sure. Along the lines of:
>
> while (*p++ = *q++)
> ;
Yes.
/* strcpy: copy t to s; pointer version 3 */
void strcpy(char *s, char *t)
{
while (*s++ = *t++)
;
}

You can see how that implementation of strcpy
would be undefined with something like:

char char_array[] = "xx\0";

strcpy(char_arr ay, char_array + 1);

though

memmove(char_ar ray, char_array + 1, 1 + strlen(char_arr ay + 1));

would be fine.
Why? How? I cannot see anything undefined in that.
If you wrote "strcpy(char_ar ray + 1, char_array);", then I would
agree.

That's what I meant write.


I must be going mad, or missing something here - are you saying that
the code below gives UB?

#include <stdio.h>

void strcpy(char *s, char *t)
{
while(*s++ = *t++)
;
}
int main(void)
{
char char_array[] = "xx\0";

strcpy(1 + char_array, char_array);

return 0;
}

If 'yes', can you explain why please.

while(*s++ = *t++)

As far as I can see, the assignment expression here will 'run' like
this:

1. eval *t

2. take value found in '1' - write to *s

3. increment s then t, OR, increment t then s.

4. repeat.

Because the destination of the assignment is within the source,
corrupting it.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Apr 2 '06 #23
Joe Wright wrote:

pemo wrote:

I must be going mad, or missing something here - are you saying that
the code below gives UB?

#include <stdio.h>

void strcpy(char *s, char *t)
{
while(*s++ = *t++)
;
}
int main(void)
{
char char_array[] = "xx\0";

strcpy(1 + char_array, char_array);

return 0;
}

If 'yes', can you explain why please.

while(*s++ = *t++)

As far as I can see, the assignment expression here will 'run' like
this:

1. eval *t

2. take value found in '1' - write to *s

3. increment s then t, OR, increment t then s.

4. repeat.

Because the destination of the assignment is within the source,
corrupting it.


Specifically, the null characters are being overwritten,
and the loop never ends.

--
pete
Apr 2 '06 #24
pete wrote:

Joe Wright wrote:

pemo wrote:

I must be going mad, or missing something here
- are you saying that
the code below gives UB?

#include <stdio.h>

void strcpy(char *s, char *t)
{
while(*s++ = *t++)
;
}
int main(void)
{
char char_array[] = "xx\0";

strcpy(1 + char_array, char_array);

return 0;
}

If 'yes', can you explain why please.

while(*s++ = *t++)

As far as I can see,
the assignment expression here will 'run' like
this:

1. eval *t

2. take value found in '1' - write to *s

3. increment s then t, OR, increment t then s.

4. repeat.

Because the destination of the assignment is within the source,
corrupting it.


Specifically, the null characters are being overwritten,
and the loop never ends.


There's also a reserved identifier problem,
but that's another story.

--
pete
Apr 2 '06 #25
Eric Sosman wrote:
CBFalconer wrote:
pete wrote:

... snip ...
As long as (*p) is defined and (*q) is defined,
there is nothing wrong with (*p++ = *q++).


Assuming p != q.


Even if p == q. (Confession: I very nearly made
the same mistake.)


Alright, modify the assumption to:

&p !- &q

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell. org/google/>
Also see <http://www.safalra.com/special/googlegroupsrep ly/>
Apr 2 '06 #26
didnt get this part

modify assumption to
&p !- &q
what is wrong in the "!= "?

Apr 2 '06 #27
"blufox" <pr************ *@gmail.com> wrote in message
news:11******** **************@ u72g2000cwu.goo glegroups.com.. .
didnt get this part

modify assumption to
&p !- &q
what is wrong in the "!= "?


Quote appropriate context when replying. As to your question, it was just a
typo.
Apr 2 '06 #28
CBFalconer wrote:

Eric Sosman wrote:
CBFalconer wrote:
pete wrote:

... snip ...

As long as (*p) is defined and (*q) is defined,
there is nothing wrong with (*p++ = *q++).

Assuming p != q.


Even if p == q. (Confession: I very nearly made
the same mistake.)


Alright, modify the assumption to:

&p !- &q


&p != &q

OK

--
pete
Apr 2 '06 #29
pete wrote:
pete wrote:
Joe Wright wrote:
pemo wrote:

I must be going mad, or missing something here
- are you saying that
the code below gives UB?

#include <stdio.h>

void strcpy(char *s, char *t)
{
while(*s++ = *t++)
;
}
int main(void)
{
char char_array[] = "xx\0";

strcpy(1 + char_array, char_array);

return 0;
}

If 'yes', can you explain why please.

while(*s++ = *t++)

As far as I can see,
the assignment expression here will 'run' like
this:

1. eval *t

2. take value found in '1' - write to *s

3. increment s then t, OR, increment t then s.

4. repeat.
Because the destination of the assignment is within the source,
corrupting it.


Specificall y, the null characters are being overwritten,
and the loop never ends.

There's also a reserved identifier problem,
but that's another story.


From what I can see there's no UB here, since it's not the standard
library function strcpy, which is indeed said to behave undefined if the
objects overlap.
This function is (apart from using a reserved identifier) perfectly
valid. Even swapping the arguments would still make this code valid,
although the behaviour of the function might not be what one would want.

Bas
Apr 3 '06 #30

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

Similar topics

2
13133
by: RU | last post by:
Hi, I am working on a porting project to port C/C++ application from unixware C++, AT&T Standard components to g++ with STL on Linux. This application has been working properly on Unixware/C++/AT&T componets environment. I have been able to compile all modules after making necessary changes in LINUX/gcc/STL environment. We have two templates defined XList and XMap.
1
11276
by: Stu | last post by:
I am trying to build the xerces shared library with 2.3.0 version of their source code on an AIX 5.1 32 bit machine with the following version of the g++ compiler /usr/local/bin/g++ -v Reading specs from /usr/local/lib/gcc-lib/powerpc-ibm-aix5.1.0.0/3.3.1/specs Configured with: ../gcc-3.3.1/configure --disable-nls Thread model: aix gcc version 3.3.1
4
6708
by: Mike | last post by:
I am having a problem when a field is spaces being undefined. I wasn't sure if the problem was Excel or Javascript, so I thought I would post here first. The users are able to select from a drop down list either a pre-existing Excel spreadsheet or a blank spreadsheet where they can enter the data. When they click the Store button I am using Javascript to validate the fields. If a particular field is not entered or has invalid data, then...
1
3161
by: Codemutant | last post by:
**** Post for FREE via your newsreader at post.usenet.com **** I just cannot find what is undefined in this code.
1
9625
by: Foolster41 | last post by:
I'm rather new to C++ programing. I'm using the dev-C++ program on a windows XP OS. I'm trying to compile the code for a multi user dungeon (MUD) called circle-mud. When I compile I get the following errors: ---- Compiler: Default compiler Building Makefile: "C:\EvoMud\circle-3.1\src\Makefile.win" Executing make...
13
3101
by: Don Vaillancourt | last post by:
What's going on with Javascript. At the beginning there was the "undefined" value which represented an object which really didn't exist then came the null keyword. But yesterday I stumbled across "null" string. I know that I will get an "undefined" when I try to retrieve something from the DOM which doesn't exist. I have used null myself to initialize or reset variables. But in which
4
4171
by: Chris Beall | last post by:
If you want your code to be bulletproof, do you have to explicitly check for the existence of any possibly-undefined variable? Example: window.outerHeight is defined by some browsers, but not others. It would therefore seem prudent, before using this variable, to do something like: if (typeof (window.outerHeight) != "undefined") { do stuff that refers to this variable } else { work around the fact that the variable isn't defined }
49
14584
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.
3
789
by: Michael Sgier | last post by:
Hi i get thousands of messages like below. How shall i resolve that? Thanks Mcihael Release/src/Utility/RawImage.o: In function `CMaskImage::CMaskImage(int, int, char const*)': RawImage.cpp:(.text+0x15dc): undefined reference to `popen(PFS*, char const*)'
45
4885
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 :-)
0
9997
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
11279
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...
1
10981
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
10499
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...
0
7205
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
5893
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...
0
6085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4307
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3323
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.