Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Q: "Overlapping Objects"

2 views
Skip to first unread message

William Kaufman

unread,
Apr 14, 1996, 3:00:00 AM4/14/96
to
In article <317142...@micromedia.on.ca> Richard Steadman <rste...@micromedia.on.ca> writes:
] Hello.
]
] I have a question which is quite simple, but I can't seem to find
] the right answer anywhere.
]
] I'm trying to figure out when it is necessary to use the memmove()
] function when copying strings. K&R and the man page says to use it
] when the "objects overlap". But I can't find a formal definition
] for this phrase (it's not in the FAQ either -- I checked!).

If you've got two pointers into the same piece of memory, they
overlap if either pointer is in the middle of the other's region (i.e.,
between the other pointer and that pointer plus the size you're
copying).

] Here's the scenario: I have a string, and want to copy the tail of
] it to an earlier part of the string. The two pieces are *not*
] overlappng when the function is called, but they may be when the
] copy is finished.
]
] Given this, it safe to use strcat() instead of memmove()?

I'm not sure I understand--they either overlap or they don't, and
asking _at what time_ fixed chunks of memory overlap doesn't make sense
to me. But I think your answer is "No".

-- Bill K.

Bill Kaufman | "I mean ... it's not even been a two-and-two-make-
wkau...@us.oracle.com | five sort of day, it's more like a two-and-two-
| make ... *fish* ..." -- "Cages", Dave McKean

Richard Steadman

unread,
Apr 14, 1996, 3:00:00 AM4/14/96
to
Hello.

I have a question which is quite simple, but I can't seem to find
the right answer anywhere.

I'm trying to figure out when it is necessary to use the memmove()
function when copying strings. K&R and the man page says to use it
when the "objects overlap". But I can't find a formal definition
for this phrase (it's not in the FAQ either -- I checked!).

Here's the scenario: I have a string, and want to copy the tail of


it to an earlier part of the string. The two pieces are *not*
overlappng when the function is called, but they may be when the
copy is finished.

Given this, it safe to use strcat() instead of memmove()?

Thanks,

Richard Steadman
rste...@mmltd.com

David Williams

unread,
Apr 16, 1996, 3:00:00 AM4/16/96
to
Just to clarify what I meant:

Given

char s[]="abcdefghijklmnop";
char *sp1=&s[2];
char *sp2=&s[4];

if you strcpy(sp1,sp2) [not strcat(), as below], will the results be
as expected ("abefghijklmnop"), or will there be problems because the copy
is copying over part of what it was originally copying from?

William Kaufman wrote:
>
> In article <317142...@micromedia.on.ca> Richard Steadman
> <rste...@micromedia.on.ca> writes:

> ] Hello.


> ]
> ] I have a question which is quite simple, but I can't seem to find
> ] the right answer anywhere.
> ]
> ] I'm trying to figure out when it is necessary to use the memmove()
> ] function when copying strings. K&R and the man page says to use it
> ] when the "objects overlap". But I can't find a formal definition
> ] for this phrase (it's not in the FAQ either -- I checked!).
>

> If you've got two pointers into the same piece of memory, they
> overlap if either pointer is in the middle of the other's region (i.e.,
> between the other pointer and that pointer plus the size you're
> copying).
>

> ] Here's the scenario: I have a string, and want to copy the tail of


> ] it to an earlier part of the string. The two pieces are *not*
> ] overlappng when the function is called, but they may be when the
> ] copy is finished.
> ]
> ] Given this, it safe to use strcat() instead of memmove()?
>

Chuck Karish

unread,
Apr 16, 1996, 3:00:00 AM4/16/96
to
In article <31739F...@micromedia.on.ca>,

David Williams <dwil...@micromedia.on.ca> wrote:
>Given
>
> char s[]="abcdefghijklmnop";
> char *sp1=&s[2];
> char *sp2=&s[4];
>
>if you strcpy(sp1,sp2) [not strcat(), as below], will the results be
>as expected ("abefghijklmnop"), or will there be problems because the copy
>is copying over part of what it was originally copying from?

The Standard C prototype for strcpy() looks like:

char *strcpy(char *s1, const char *s2);

This guarantees that the data pointed to by s2 will not be
modified by the call to strcpy(). Since this guarantee
cannot be fulfilled if the strings overlap as shown above,
the description of strcpy() says:

If copying takes place between objects that ovewrlap, the
behavior is undefined.

The exact mechanism by which the strcpy is done is not specified
in any standard I know of. That means that while this code
will probably work on some systems, it's not guaranteed to
work everywhere.
--

Chuck Karish kar...@mindcraft.com
(415) 323-9000 x117 kar...@pangea.stanford.edu

Lawrence Kirby

unread,
Apr 26, 1996, 3:00:00 AM4/26/96
to

In article <4l0knt$l...@nntp.Stanford.EDU>
kar...@pangea.Stanford.EDU "Chuck Karish" writes:

>In article <31739F...@micromedia.on.ca>,
>David Williams <dwil...@micromedia.on.ca> wrote:
>>Given
>>
>> char s[]="abcdefghijklmnop";
>> char *sp1=&s[2];
>> char *sp2=&s[4];
>>
>>if you strcpy(sp1,sp2) [not strcat(), as below], will the results be
>>as expected ("abefghijklmnop"), or will there be problems because the copy
>>is copying over part of what it was originally copying from?

The result is undefined because source and destination overlap:

"abcdefghijklmnop"
Source: sssssssssssss (includes terminating null character)
Destination: ddddddddddddd
Overlap: ooooooooooo


>The Standard C prototype for strcpy() looks like:
>
> char *strcpy(char *s1, const char *s2);
>
>This guarantees that the data pointed to by s2 will not be
>modified by the call to strcpy().

No, it guarantees that s2 won't be used to modify data. There's nothing
in the prototype itself that prohibits what s2 is pointing to being
modified through another source e.g. s1. For example:

void *memmove(void *s1, const void *s2, size_t n);

Here s1 and s2 are allowed to point to overlapping objects - what s2
points to could end up modified (but not using s2).

> Since this guarantee
>cannot be fulfilled if the strings overlap as shown above,
>the description of strcpy() says:

No, the prototype implies no such guarantee which is why...

> If copying takes place between objects that ovewrlap, the
> behavior is undefined.

...the standard states it explicitly.

>
>The exact mechanism by which the strcpy is done is not specified
>in any standard I know of. That means that while this code
>will probably work on some systems, it's not guaranteed to
>work everywhere.

For example the string could be copied from the end backwards.

--
-----------------------------------------
Lawrence Kirby | fr...@genesis.demon.co.uk
Wilts, England | 7073...@compuserve.com
-----------------------------------------

0 new messages