C语言 为什么“memset(arr, -1, sizeof(arr)/sizeof(int))”不能将整数数组清除为-1?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7202411/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:29:55  来源:igfitidea点击:

Why does "memset(arr, -1, sizeof(arr)/sizeof(int))" not clear an integer array to -1?

carraysmemoryintegermemset

提问by Ravi Gupta

Is it not possible to use memseton an array of integers? I tried the following memsetcall and didn't get the correct integer values in the intarray.

不能memset在整数数组上使用吗?我尝试了以下memset调用,但没有在int数组中获得正确的整数值。

int arr[5];
memset (arr, -1, sizeof(arr)/sizeof(int));

Values I got are:

我得到的值是:

arr[0] = -1
arr[1] = 255
arr[2] = 0
arr[3] = 0
arr[4] = 0

回答by Ioan Paul Pirau

Just change to memset (arr, -1, sizeof(arr));

只需更改为 memset (arr, -1, sizeof(arr));

Note that for other values than 0 and -1 this would not worksince memsetsets the byte values for the block of memory that starts at the variable indicated by *ptrfor the following numbytes.

请注意,对于 0 和 -1 以外的其他值,这将不起作用,因为memset设置了从*ptr以下num字节指示的变量开始的内存块的字节值。

void * memset ( void * ptr, int value, size_t num );

And since intis represented on more than one byte, you will not get the desired value for the integers in your array.

并且由于int以多个字节表示,因此您将无法获得数组中整数的所需值。

Exceptions:

例外:

  • 0 is an exception since, if you set all the bytes to 0, the value will be zero
  • -1 is another exception since, as Patrick highlighted -1 is 0xff (=255) in int8_t and 0xffffffff in int32_t
  • 0 是一个例外,因为如果您将所有字节设置为 0,则该值将为零
  • -1 是另一个例外,因为帕特里克强调 -1 是 int8_t 中的 0xff (=255) 和 int32_t 中的 0xffffffff

The reason you got:

你得到的原因:

arr[0] = -1
arr[1] = 255
arr[2] = 0
arr[3] = 0
arr[4] = 0

Is because, in your case, the length of an int is 4 bytes (32 bit representation), the length of your array in bytes being 20 (=5*4), and you only set 5 bytes to -1 (=255) instead of 20.

是因为,在您的情况下,int 的长度为 4 个字节(32 位表示),您的数组长度(以字节为单位)为 20(=5*4),而您只将 5 个字节设置为 -1(=255)而不是 20。

回答by Sander De Dycker

Don't use memsetto initialize anything else than single-byte data types.

不要memset用于初始化单字节数据类型以外的任何内容。

At first sight, it might appear that it should work for initializing an intto 0or -1(and on many systems it will work), but then you're not taking into account the possibility that you might generate a trap representation, causing undefined behavior, or the fact that the integer representation is not necessarily two's complement.

乍一看,它似乎应该适用于初始化intto0-1(并且在许多系统上它可以工作),但是您没有考虑可能会生成陷阱表示,导致未定义的行为,或者整数表示不一定是二进制补码的事实。

The correct way to initialize an array of intto -1, is to loop over the array, and set each value explicitly.

初始化的阵列正确的方法int-1,是循环阵列之上,并显式地设置每个值。

回答by Faiz Halde

gcc provides a good array initialization shortcut

gcc 提供了一个很好的数组初始化快捷方式

int arr[32] = {[0 ... 10] = 3, [11 ... 31] = 4}

int arr[32] = {[0 ... 10] = 3, [11 ... 31] = 4}

mind the space before and after ...

注意前后空间 ...

回答by Rudy Velthuis

Why the division?

为什么要划分?

memset(arr, -1, sizeof(arr));

Your version, sizeof(arr)/sizeof(int), gives you the number of elements in the array.

您的版本sizeof(arr)/sizeof(int)为您提供数组中的元素数。

回答by Bo Persson

You can save yourself some typing by initializing the array directly:

您可以通过直接初始化数组来节省一些输入:

int arr[5] = {-1, -1, -1, -1, -1}; 

That line is shorter than the memset, and it also works.

那条线比 memset 短,它也能用。

回答by kingo

void * memset ( void * ptr, int value, size_t num );

This function works well on most systems when applied to set char array. It sets the first num BYTES of the block of memory pointed by ptr to the specified value (interpreted as an unsigned char). memset-C++ ReferenceIt operates one byte each time. So it works fine if you assign the second arguments with a int value no more than 0xff.

当应用于设置字符数组时,此函数在大多数系统上运行良好。它将 ptr 指向的内存块的第一个 num BYTES 设置为指定值(解释为无符号字符)。memset-C++ 参考每次操作一个字节。因此,如果您为第二个参数分配一个不超过 0xff 的 int 值,它就可以正常工作。

As for your version, the third arguments is the number of array elements, so you got your output. Actually the truth is you are supposed to assign the third arguments the NUMBER OF BYTES you want.

至于你的版本,第三个参数是数组元素的数量,所以你得到了你的输出。实际上,您应该为第三个参数分配您想要的字节数。

So the correct version should be like this:

所以正确的版本应该是这样的:

memset (arr, -1, sizeof(arr));