Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
646 views
in Technique[技术] by (71.8m points)

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

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?

question from:https://stackoverflow.com/questions/18330673/bzero-bcopy-versus-memset-memcpy

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...