Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bzero() & bcopy() versus memset() & memcpy()

Tags:

c

linux

Is there any reason to use the non-standard bzero() and bcopy() instead of memset() and memcpy() in a Linux environment? I've heard many say that they're better for Linux compilers, but haven't seen any advantages over the standard functions.

Are they more optimized than the standard ones, or do they have any behavioral particularity for which they're preferred?

like image 965
Iosif Murariu Avatar asked Aug 20 '13 08:08

Iosif Murariu


People also ask

What is Bzero?

The bzero() function erases the data in the n bytes of the memory starting at the location pointed to by s, by writing zeros (bytes containing '\0') to that area. The explicit_bzero() function performs the same task as bzero().

Why is Bzero used?

The bzero Function This function is used to set all the socket structures with null values. void bzero(void *s, int nbyte); This function does not return anything.

What is the difference between Bzero and memset?

memset() takes 3 arguments, a pointer, a value, and a length. bzero() , on the other hand, omits the value parameter, since it's implied to be zero. memset() 's documentation provides one somewhat worrying detail though: The memset() function writes n bytes of value c (converted to an unsigned char) to the string s.

What is memset in c?

The memset() function sets the first count bytes of dest to the value c. The value of c is converted to an unsigned character.


3 Answers

While bzero and bcopy functions aren't ISO C (the actual standard that I assume you're talking about when referring to them as non-standard), they were a POSIX standard thing, although they pre-dated both ISO and POSIX.

And note that use of the word "were" - these functions were deprecated in POSIX.1-2001 and fianally removed in POSIX.1-2008, in deference to memset, memcpy and memmove. So you're better off using the standard C functions where possible.

If you have a lot of code that uses them and you don't want to have to go and change it all (though you probably should at some point), you can use the following quick substitutions:

// void bzero(void *s, size_t n);
#define bzero(s, n) memset((s), 0, (n))

// void bcopy(const void *s1, void *s2, size_t n);
#define bcopy(s1, s2, n) memmove((s2), (s1), (n))
like image 128
paxdiablo Avatar answered Oct 07 '22 12:10

paxdiablo


#include <strings.h>
void bcopy(const void *src, void *dest, size_t n);

Description The bcopy() function copies n bytes from src to dest. The result is correct, even when both areas overlap.

Conforming to: 4.3BSD, it seems b comes from BSD and it seems deprecated.

Which means bcopy is analogous to memmove() not memcpy() as R.. said at his comment.

Note: strings.h is also distinct from string.h.

like image 35
the kamilz Avatar answered Oct 07 '22 11:10

the kamilz


Actually nowdays it could be the other way around. You see that because memcpy and memset is included in the standard the compiler will be allowed to assume that function called so does exactly what the standard prescribes. This means that the compiler can replace them with the most efficient way of performing the operation it can figure out. With bcopy and bzero on the other hand the standard does not prescribe any behavior on those so the compiler can't assume anything - which means that the compiler would need to issue an actual function call.

However GCC for example knows about bcopy and bzero if it's built for an OS that have them.

like image 2
skyking Avatar answered Oct 07 '22 11:10

skyking