Portability of some overlapping strcpy or memcpy calls

David H. Wolfskill david at dhw68k.cts.com
Fri Mar 10 23:34:56 AEST 1989


In article <338 at wjh12.harvard.edu> kendall%saber at harvard.harvard.edu (Samuel C. Kendall) writes:
>[describes an overlapping memcpy() call....]

>I am interested in finding out if this call to memcpy, and similar
>calls to memccpy, strcpy, and strncpy, are portable.

>I'm interested in practical portability, not theoretical.  This call is
>obviously illegal....

>Details: how could this call fail to copy correctly?  The
>implementations I know copy left-to-right.  An implementation that
>copied right-to-left would definitely fail.

Well, there exists at least one machine I know of that is designed so
the hardware *does* move right-to-left: the IBM System/3 and its
descendants (such as the s/36).

Furthermore, fields are addressed by their right-most bytes (except for
one instruction involving (I think -- it's been several years since I
had anything to do with the braindamaged boxes) "editing" numeric
quantities for output.

Certainly a C compiler (and I have heard reports that a C compiler
exists for the s/36 -- though the compiler runs on an IBM-PC, and
generates code for the s/36... that probably says something about trying
to do anything useful with a s/36....) could mask that behavior from the
programmer, but it certainly would not be able to transform such a call
to a single "MVC" ("move characters") machine instruction -- and have it
do what (I presume) you want.

   op 1
||||||||||
abcdefghijkl      ===>   klklklklklkl
  ||||||||||
     op 2

is what it *would* do... and if that's what you wanted, that would seem
to be OK (if slightly peculiar).  (If the difference between the
addresses is 1, it is a method of clearing a field so each character has
the same value.)

The call, as Samuel pointed out, is not legal [p?]ANS C; that was hashed
out some time ago.  (No, I don't want to go into the discussion again.
Suffice it to say that I wanted the compiler/machine's behavior
*documented*, rather than just outlawing the call.  memmove() supports
what you want.)

Hope I haven't stirred to many embers up....

:-),
david
-- 
David H. Wolfskill
uucp: ...{spsd,zardoz,felix}!dhw68k!david	InterNet: david at dhw68k.cts.com



More information about the Comp.lang.c mailing list