Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between memmove and bcopy?

Tags:

c

memmove

I understand the difference between memcpy and memmove: memmove handles the overlapping of src and dst. I checked the man page for bcopy and it seems to also handle overlapping. So I'm wondering if there is any difference between memmove and bcopy?

like image 338
Tsak Avatar asked Dec 31 '22 17:12

Tsak


2 Answers

bcopy and memmove do exactly the same thing. However, they take arguments in a different order: bcopy(src, dest, n) vs memmove(dest, src, n). So they can't be two names for the same function.

Historically, bcopy is the older of the two; it was present in 3BSD if memory serves. memmove was invented by the C committee in 1988 or so.

New code should use memmove, as it is required by the C standard and therefore more portable than bcopy.

like image 200
zwol Avatar answered Jan 09 '23 18:01

zwol


The main difference normally comes from their origin. bcopy comes from Berkeley Source Distribution (BSD) systems, while memcpy and their family come mainly from AT&T's System V. And finally, by complying with legacy code, all those functions were included in the standards.

There's a good explanation about the use of memset vs bzero in Richard Steven's book "UNIX network programming" that somewhat explains about the use of either one.

Today implementations just resolve to calling the same internal implementation but providing both interfaces.

like image 30
Luis Colorado Avatar answered Jan 09 '23 19:01

Luis Colorado