How memset initializes an array of integers by -1?

后端 未结 2 904
轮回少年
轮回少年 2020-12-13 12:13

The manpage says about memset:

#include 
void *memset(void *s, int c, size_t n)

The me

相关标签:
2条回答
  • 2020-12-13 12:45

    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 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 -1 signed ints, longs, and shorts on two's complement hardware.

    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.

    0 讨论(0)
  • 2020-12-13 12:49

    When all bits of a number are 0, its value is also 0. However, if all bits are 1 the value is -1.

    If we write int a[2], 4x2 bytes of memory is allocated which contains random/garbage bits-

    00110000 00100101 11100011 11110010    11110101 10001001 00111000 00010001
    

    Then, we write memset(a, 0, sizeof(a)). Now, memset() works byte by byte, and one byte representation (unsigned char) of 0 is 00000000. So, it becomes-

    00000000 00000000 00000000 00000000    00000000 00000000 00000000 00000000
    

    Therefore, both a[0] and a[1] are initialized with 0.


    Now, lets see memset(a, -1, sizeof(a)): one byte for -1 is 11111111. And, we get-

    11111111 11111111 11111111 11111111    11111111 11111111 11111111 11111111
    

    Here, both a[0] and a[1] will have the value -1.


    However, for memset(a, 1, sizeof(a)): 1 in a byte is 00000001-

    00000001 00000001 00000001 00000001    00000001 00000001 00000001 00000001
    

    So, the value will be- 16843009.

    0 讨论(0)
提交回复
热议问题