简体   繁体   English

为什么在memset中使用'\\ 0'而不是0?

[英]Why use '\0' instead of 0 for memset?

According to the this Unix documentation http://pubs.opengroup.org/onlinepubs/009695399/functions/bzero.html 根据这个Unix文档http://pubs.opengroup.org/onlinepubs/009695399/functions/bzero.html

The memset() function is preferred over bzero().

For maximum portability, it is recommended to replace 
the function call to bzero() as follows:

#define bzero(b,len) (memset((b), '\0', (len)), (void) 0)


But void *memset(void *s, int c, size_t n); 但是void *memset(void *s, int c, size_t n); second arg is an int so why are they recommending '\\0' instead of 0? 第二个arg是一个int ,为什么他们推荐'\\ 0'而不是0? The memset() doc says "The memset() function shall copy c ( converted to an unsigned char ) arg into each of the first n bytes of the object pointed to by s ." memset()doc说“memset()函数应将c转换为unsigned char )arg复制到s指向的对象的前n个字节中。” Is this more efficient or simply more explicit? 这更有效还是更明确? Is there a best practice involved here? 这里有最佳实践吗?

second arg is an int so why are they recommending '\\0' instead of 0? 第二个arg是一个int,为什么他们推荐'\\ 0'而不是0?

To make it clear the thing is going to end up as a NUL character; 要说清楚,事情最终会成为一个NUL角色; after all memset will convert it to unsigned char . 毕竟memset会将它转换为unsigned char This is just a matter of style, because '\\0' has type int and is thus the same as 0 . 这只是样式问题,因为'\\0'类型为int ,因此与0相同。

Is it more efficient? 它效率更高吗?

In a way: bzero was never actually part of the standard. 在某种程度上: bzero实际上从未成为标准的一部分。 That's why the page you link to recommends using memset . 这就是您链接到的页面建议使用memset So using memset is more efficient in the sense that you can rest assured that your code compiles on all standard C compilers. 因此,使用memset更有效,因为您可以放心,您的代码将编译在所有标准C编译器上。

'\\0' instead of 0 '\\0'而不是0
The standard requires that a char can be safely converted to an int: 该标准要求char可以安全地转换为int:

Both the basic source and basic execution character sets shall have the following members: 基本源代码和基本执行字符集都应具有以下成员:
[...] [...]
the 10 decimal digits 10位十进制数字
0 1 2 3 4 5 6 7 8 9
[...] [...]
In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous. 在源和执行基本字符集中,上述十进制数字列表中0之后的每个字符的值应比前一个值大1。

So passing '\\0' to an int param is not a problem. 因此将'\\0'传递给int参数不是问题。 What's more: you are passing a zero-escape-sequence , which is actually the same as passing 0. 更重要的是:你传递的是一个零转义序列 ,它实际上与传递0相同。
It is, however, more explicit: memset will initialize N bytes to whatever value you pass to it. 但是,它更明确: memset会将N个字节初始化为传递给它的任何值。 The only type in C that is guaranteed to be 1 and only 1 byte big, is the char type. C中唯一保证为1且只有1字节大的类型是char类型。

So: 所以:

memset(some_ptr, 0, 12);
//is, in practice identical to:
memset(some_ptr, '\0', 12);

But seeing as you're setting 12 bytes, passing a value of a 1-byte-big type better reflects this. 但是当你设置12个字节时,传递一个1字节大的类型的值会更好地反映这一点。 I prefer passing a char explicitly for that reason, but it's up to you. 由于这个原因,我更喜欢明确传递一个字符,但这取决于你。

History of memset : memset历史:
The function memset has been around for ages. 功能memset已存在很长时间了。 In fact, it existed back in the days the function prototypes! 事实上,它存在于功能原型的时代!

Another thing to keep in mind is how character literals in C, by themselves, are, like most any literal value, ints. 要记住的另一件事是,C中的字符文字本身就像大多数字面值一样,是int。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM