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 firstn
bytesof the memory area pointed to bys
with the constant bytec
.
#include <string.h> void *memset(void *s, int c, size_t n)
的
memset()
功能填充第一n
字节由指向的存储器区域的s
具有恒定字节c
。
It is obvious that memset
can't be used to initialize int
array as shown below:
很明显,memset
不能用于初始化int
数组,如下所示:
int a[10];
memset(a, 1, sizeof(a));
it is because int
is 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 memset
to set the int
array elements to either 0
or -1
.
这是因为int
由 4 个字节表示(比如说),并且无法获得 array 中整数的所需值a
。
但是我经常看到程序员使用 memset
将int
数组元素设置为0
or 或-1
。
int a[10];
int b[10];
memset(a, 0, sizeof(a));
memset(b, -1, sizeof(b));
As per my understanding, initializing with integer 0
is OK because 0
can be represented in 1 byte (may be I am wrong in this context). But how is it possible to initialize b
with -1
(a 4 bytes value)?
根据我的理解,用整数初始化0
是可以的,因为0
可以用 1 个字节表示(在这种情况下我可能错了)。但是如何b
用-1
(4 个字节的值)进行初始化?
回答by dasblinkenlight
Oddly, the reason this works with -1
is exactly the same as the reason that this works with zeros: in two's complement binary representation, -1
has 1
s in all its bits, regardless of the size of the integer, so filling in a region with bytes filled with all 1
s produces a region of -1
signed int
s, long
s, and short
s on two's complement hardware.
奇怪的是,之所以这个作品用-1
是完全一样的,这个作品以零的原因:在二进制补码表示,-1
拥有1
S IN的所有位,无论整数的大小,在一个地区做馅用字节填充all 1
s在二进制补码硬件上生成一个带-1
符号的int
s、long
s 和short
s区域。
On hardware that differs from two's complement the result will be different. The -1
integer constant would be converted to an unsigned char
of 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 1
would 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 1
the 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。