问题描述
在一个系统编程类我把这个previous学期,我们必须实现C.一个基本的客户机/服务器初始化时结构,如 sock_addr_in
,或字符缓冲区(我们使用的客户端和服务器之间发送数据来回)教授指示我们只使用 bzero
,而不是 memset的
初始化它们。他从不解释原因,而且我很好奇,如果有这种正当的理由?
In a Systems Programming class I took this previous semester, we had to implement a basic client/server in C. When initializing the structs, like sock_addr_in
, or char buffers (that we used to send data back and forth between client and server) the professor instructed us to only use bzero
and not memset
to initialize them. He never explained why, and I'm curious if there is a valid reason for this?
我在这里看到:http://fdiv.net/2009/01/14/memset-vs-bzero-ultimate-showdown该 bzero
更有效,由于一个只能将要归零记忆的事实,所以它没有做任何额外的检查 memset的
可以这样做。这仍然并不一定看起来像一个理由绝对不是用 memset的
因为虽然零内存。
I see here: http://fdiv.net/2009/01/14/memset-vs-bzero-ultimate-showdown that bzero
is more efficient due to the fact that is only ever going to be zeroing memory, so it doesn't have to do any additional checking that memset
may do. That still doesn't necessarily seem like a reason to absolutely not use memset
for zeroing memory though.
bzero
被认为是德precated,此外是不是一个标准的C函数。根据手册, memset的
是pferred超过 bzero
为此$ P $。所以,你为什么要仍然使用 bzero
在 memset的
?只是为了提高效率,或者是更多的东西?同样,什么是 memset的
超过 bzero
,使它成为事实上的$ P $为新的程序pferred选项的好处?
bzero
is considered deprecated, and furthermore is a not a standard C function. According to the manual, memset
is preferred over bzero
for this reason. So why would you want to still use bzero
over memset
? Just for the efficiency gains, or is it something more? Likewise, what are the benefits of memset
over bzero
that make it the de facto preferred option for newer programs?
推荐答案
我看不出有任何理由要preFER bzero
在 memset的
。
I don't see any reason to prefer bzero
over memset
.
memset的
是一个标准的C函数,而 bzero
从未有过的C标准的功能。理由可能是因为你可以使用实现完全相同的功能 memset的
功能。
memset
is a standard C function while bzero
has never been a C standard function. The rationale is probably because you can achieve exactly the same functionality using memset
function.
现在关于效率,编译器像 GCC
使用内置的实现为 memset的
其切换到特定的实现,当一个常数 0
进行检测。同为的glibc
时,建宏被禁用。
Now regarding efficiency, compilers like gcc
use builtin implementations for memset
which switch to a particular implementation when a constant 0
is detected. Same for glibc
when builtins are disabled.
这篇关于为什么在memset的使用bzero?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!