C++ memset 如何用 -1 初始化整数数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24207698/
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
How memset initializes an array of integers by -1?
提问by haccks
The manpagesays about memset:
该手册页说,约memset:
#include <string.h> void *memset(void *s, int c, size_t n)The
memset()function fills the firstnbytesof the memory area pointed to byswith the constant bytec.
#include <string.h> void *memset(void *s, int c, size_t n)的
memset()功能填充第一n字节由指向的存储器区域的s具有恒定字节c。
It is obvious that memsetcan't be used to initialize intarray as shown below:
很明显,memset不能用于初始化int数组,如下所示:
int a[10];
memset(a, 1, sizeof(a));
it is because intis represented by 4 bytes (say) and one can not get the desired value for the integers in array a.
But I often see the programmers use memsetto set the intarray elements to either 0or -1.
这是因为int由 4 个字节表示(比如说),并且无法获得 array 中整数的所需值a。
但是我经常看到程序员使用 memset将int数组元素设置为0or 或-1。
int a[10];
int b[10];
memset(a, 0, sizeof(a));
memset(b, -1, sizeof(b));
As per my understanding, initializing with integer 0is OK because 0can be represented in 1 byte (may be I am wrong in this context). But how is it possible to initialize bwith -1(a 4 bytes value)?
根据我的理解,用整数初始化0是可以的,因为0可以用 1 个字节表示(在这种情况下我可能错了)。但是如何b用-1(4 个字节的值)进行初始化?
回答by dasblinkenlight
Oddly, the reason this works with -1is exactly the same as the reason that this works with zeros: in two's complement binary representation, -1has 1s in all its bits, regardless of the size of the integer, so filling in a region with bytes filled with all 1s produces a region of -1signed ints, longs, and shorts on two's complement hardware.
奇怪的是,之所以这个作品用-1是完全一样的,这个作品以零的原因:在二进制补码表示,-1拥有1S IN的所有位,无论整数的大小,在一个地区做馅用字节填充all 1s在二进制补码硬件上生成一个带-1符号的ints、longs 和shorts区域。
On hardware that differs from two's complement the result will be different. The -1integer constant would be converted to an unsigned charof all ones, because the standard is specific on how the conversion has to be performed. However, a region of bytes with all their bits set to 1would be interpreted as integral values in accordance with the rules of the platform. For example, on sign-and-magnitude hardware all elements of your array would contain the smallest negative value of the corresponding type.
在不同于二进制补码的硬件上,结果会有所不同。的-1整数常数将被转换成一个unsigned char全部为,因为标准是特定的转换必须如何进行。但是,1根据平台规则,所有位都设置为 的字节区域将被解释为整数值。例如,在符号和大小硬件上,数组的所有元素都将包含相应类型的最小负值。
回答by Minhas Kamal
When all bits of a number are 0, its value is also 0. However, if all bits are 1the value is -1.
当一个数的所有位都为 时0,其值也为0。但是,如果所有位1值是-1。
When we write int a[2], 4x2bytes of memory is allocated which contains random/garbage bits-
当我们写入时int a[2],分配了4x2字节的内存,其中包含随机/垃圾位-
00110000 00100101 11100011 11110010 11110101 10001001 00111000 00010001
Then we write memset(a, 0, sizeof(a)). Now, memset()does not distinguish between int& char. It works byte by byte. And one byte representation of 0is 00000000. So, we get-
然后我们写memset(a, 0, sizeof(a))。现在,memset()不区分int& char。它逐字节工作。0 的一字节表示是00000000. 所以,我们得到——
00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
Therefore, both a[0]and a[1]are initialized with 0.
因此,a[0]和a[1]都初始化为0。
Now, lets see memset(a, -1, sizeof(a)): One byte for -1is 11111111. And, we get-
现在,让我们看看memset(a, -1, sizeof(a)):-1 的一个字节是11111111. 而且,我们得到-
11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111
Here, both a[0]and a[1]will have the value -1.
在这里,a[0]和a[1]都将具有值-1。
However, for memset(a, 1, sizeof(a)): 1in a byte is 00000001-
但是,对于memset(a, 1, sizeof(a)):一个字节中的1是00000001-
00000001 00000001 00000001 00000001 00000001 00000001 00000001 00000001
So, the value will be- 16843009.
因此,该值将是- 16843009。

