[comp.unix.questions] memcpy != bcopy

peter@thirdi.UUCP (Peter Rowell) (02/12/90)

echarne@orion.oac.uci.edu (Eli B. Charne) writes:
>larryd1@attctc.Dallas.TX.US (Larry Clark) writes:
>>	I'm porting sources from the bsd world into a Sys V environment
>>	and need an explanation of bcopy() and bzero().
>> ....
> ...
>     bcopy() Copies length bytes from the "string" b1, and copies them into
>     string b2.  I was under the impression this was exactly what memcpy()
>     does, but perhaps the order of arguments is changed.

WARNING!  memcpy is NOT the same as bcopy!!!!
This is the source of an *amazing* array of hard-to-find bugs!
bcopy correctly handles overlapping memory moves; memcpy does not.

If you code does not expect/require (either deliberately or accidentally)
that overlapping moves be done correctly, then it is "safe" to claim
the two are equivalent.

PS. the first two arguments to bcopy are reversed in memcpy.

cleeland@rex.cs.tulane.edu (Chris Cleeland) (02/12/90)

In article <497@thirdi.UUCP> peter@thirdi.UUCP (Peter Rowell) writes:
>echarne@orion.oac.uci.edu (Eli B. Charne) writes:
>
>WARNING!  memcpy is NOT the same as bcopy!!!!
>This is the source of an *amazing* array of hard-to-find bugs!
>bcopy correctly handles overlapping memory moves; memcpy does not.
>
>If you code does not expect/require (either deliberately or accidentally)
>that overlapping moves be done correctly, then it is "safe" to claim
>the two are equivalent.
>
>PS. the first two arguments to bcopy are reversed in memcpy.

What he said.

Also, though, memmove() *does* properly handle overlapping areas (at
least according to K&R2 -- my system has bcopy).  Parameters are the
same as memcopy()
-- 
--------------------------------------+--------------------------------------
Chris Cleeland                        | BELL:   (504) 866-8186
INET:   cleeland@rex.cs.tulane.edu    | USnail: 1320 Lowerline, Apt. E
UUCP:   cleeland@rex.UUCP             |         New Orleans, LA  70118
Disclaimer:  "I'm a student -- I can't afford one!"

jrh@mustang.dell.com (James R. Howard) (02/13/90)

> >larryd1@attctc.Dallas.TX.US (Larry Clark) writes:
> >>	I'm porting sources from the bsd world into a Sys V environment
> >>	and need an explanation of bcopy() and bzero().
> >> ....

Okay, I have read some of the replies, but I thought I would point out
that on my SYS V
system (Dell 386/ix SVR3.2) the file libinet.a has these routines.  So,
typically when 
porting applications over, I just edit the makefile to add -linet into
the build.  This of
course may not be the case on your system, but if it is, it may save you
some time.

--------------------------------------------------------------
James Howard
..cs.utexas.edu!dell!mustang!jrh   or    jrh@mustang.dell.com

"I've got a firm policy on gun control, if there's a gun       
around, I want to be the one controlling it."          
-- Clint Eastwood 
--------------------------------------------------------------

chris@mimsy.umd.edu (Chris Torek) (02/18/90)

In article <497@thirdi.UUCP> peter@thirdi.UUCP (Peter Rowell) writes:
>WARNING!  memcpy is NOT the same as bcopy!!!!
>This is the source of an *amazing* array of hard-to-find bugs!
>bcopy correctly handles overlapping memory moves; memcpy does not.

Beware: some versions of bcopy() do, some do not.  (4.2BSD VAX bcopy()
handled overlap when copying 65535 bytes or less or when forward block
moves were correct.  4.3BSD VAX bcopy() always handles overlap.)

For absolute safety, use memmove() (a feature of ANSI X3.159 C).

Followups directed to comp.lang.c (put them somewhere else if not talking
about the C aspects of overlapping moves, please).
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris@cs.umd.edu	Path:	uunet!mimsy!chris