为什么“memset(arr, -1, sizeof(arr)/sizeof(int))”不将整数数组清除为-1?
是否无法对整数数组使用memset?我尝试了以下 memset
调用,但没有在 int
数组中获得正确的整数值。
int arr[5];
memset (arr, -1, sizeof(arr)/sizeof(int));
我得到的值是:
arr[0] = -1
arr[1] = 255
arr[2] = 0
arr[3] = 0
arr[4] = 0
Is it not possible to use memset
on an array of integers? I tried the following memset
call and didn't get the correct integer values in the int
array.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
只需更改为
memset (arr, -1, sizeof(arr));
请注意,对于 0 和 -1 以外的其他值,这将不起作用,因为 memset 设置从
指示的变量开始的内存块的字节值*ptr
用于以下内容num
字节。由于
int
用多个字节表示,因此您将无法获得数组中整数的所需值。例外:
你得到的原因是:
是因为,在你的情况下, int 的长度是 4 个字节(32 位表示),数组的长度(以字节为单位)是20(=5*4),并且您只将5个字节设置为-1(=255)而不是20。
Just change to
memset (arr, -1, sizeof(arr));
Note that for other values than 0 and -1 this would not work since memset sets the byte values for the block of memory that starts at the variable indicated by
*ptr
for the followingnum
bytes.And since
int
is represented on more than one byte, you will not get the desired value for the integers in your array.Exceptions:
The reason you got:
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.
不要使用
memset
初始化单字节数据类型以外的任何内容。乍一看,它似乎应该适用于将
int
初始化为0
或-1
(并且在许多系统上它都适用) ,但是您没有考虑到可能生成陷阱表示的可能性,从而导致未定义的行为,或者整数表示为 不一定是二进制补码。将
int
数组初始化为-1
的正确方法是循环遍历该数组,并显式设置每个值。Don't use
memset
to initialize anything else than single-byte data types.At first sight, it might appear that it should work for initializing an
int
to0
or-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.The correct way to initialize an array of
int
to-1
, is to loop over the array, and set each value explicitly.gcc提供了一个很好的数组初始化快捷方式
int arr[32] = {[0 ... 10] = 3, [11 ... 31] = 4}
注意
前后的空格...
gcc provides a good array initialization shortcut
int arr[32] = {[0 ... 10] = 3, [11 ... 31] = 4}
mind the space before and after
...
为什么要划分?
您的版本
sizeof(arr)/sizeof(int)
为您提供了数组中的元素数量。Why the division?
Your version,
sizeof(arr)/sizeof(int)
, gives you the number of elements in the array.您可以通过直接初始化数组来节省一些输入:
该行比 memset 短,而且它也可以工作。
You can save yourself some typing by initializing the array directly:
That line is shorter than the memset, and it also works.
当应用于设置字符数组时,此函数在大多数系统上运行良好。
它将 ptr 指向的内存块的第一个 num BYTES 设置为指定值(解释为无符号字符)。 memset-C++ 参考
每次操作一个字节。因此,如果为第二个参数分配不超过 0xff 的 int 值,则效果很好。
至于您的版本,第三个参数是数组元素的数量,因此您得到了输出。
实际上,事实是您应该为第三个参数分配您想要的字节数。
所以正确的版本应该是这样的:
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++ Reference
It operates one byte each time. So it works fine if you assign the second arguments with a int value no more than 0xff.
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: