Linux bzero() & bcopy() 对比 memset() & memcpy()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18330673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
bzero() & bcopy() versus memset() & memcpy()
提问by Iosif Murariu
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.
是否有任何理由在 Linux 环境中使用非标准的bzero()
andbcopy()
而不是memset()
and memcpy()
?我听到很多人说它们更适合 Linux 编译器,但没有看到比标准函数有任何优势。
Are they more optimized than the standard ones, or do they have any behavioral particularity for which they're preferred?
它们是否比标准的更优化,或者它们是否具有它们更喜欢的任何行为特殊性?
采纳答案by paxdiablo
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 werea POSIX standard thing, although they pre-dated both ISO andPOSIX.
虽然bzero
和bcopy
函数不是 ISO C(我假设您在将它们称为非标准时所谈论的实际标准),但它们是POSIX 标准的东西,尽管它们早于 ISO和POSIX。
And note that use of the word "were" - these functions were deprecated in POSIX.1-2001 and fianally removedin POSIX.1-2008, in deference to memset
, memcpy
and memmove
. So you're better off using the standard C functions where possible.
并注意使用“是”这个词 - 这些函数在 POSIX.1-2001 中被弃用,并最终在 POSIX.1-2008 中删除,以尊重memset
,memcpy
和memmove
。因此,您最好尽可能使用标准 C 函数。
If you have a lotof code that uses them and you don't want to have to go and change it all (though you probably shouldat 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))
回答by skyking
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.
实际上,现在情况可能正好相反。你会看到,因为memcpy
和memset
包含在标准中,编译器将被允许假设调用的函数完全符合标准的规定。这意味着编译器可以用最有效的方式来执行它可以找出的操作来替换它们。随着bcopy
和bzero
在另一方面的标准并没有规定这些行为的任何这样编译器不能假设任何事情-这意味着,编译器将需要发出一个实际的函数调用。
However GCC for example knows about bcopy
and bzero
if it's built for an OS that have them.
但是GCC例如知道bcopy
和bzero
如果它的建成为拥有它们的操作系统。
回答by the kamilz
#include <strings.h>
void bcopy(const void *src, void *dest, size_t n);
DescriptionThe bcopy()
function copies n bytes from src to dest. The result is correct, even when both areas overlap.
描述该bcopy()
函数将 n 个字节从 src 复制到 dest。即使两个区域重叠,结果也是正确的。
Conforming to: 4.3BSD, it seems bcomes from BSD and it seems deprecated.
符合:4.3BSD,似乎b来自 BSD 并且似乎已弃用。
Which means bcopy
is analogous to memmove()
not memcpy()
as R.. said at his comment.
这意味着bcopy
类似于memmove()
不像memcpy()
R.. 在他的评论中所说的那样。
Note: strings.h
is also distinct from string.h
.
注意:strings.h
也不同于string.h
.