Initializing entire array with memset

前端 未结 4 1163
天涯浪人
天涯浪人 2021-01-19 13:09

I have initialised the entire array with value 1 but the output is showing some garbage value. But this program works correctly if i use 0 or -1 in place of 1. So are there

4条回答
  •  我在风中等你
    2021-01-19 14:03

    Memset fills bytes, from cppreference:

    Converts the value ch to unsigned char and copies it into each of the first count characters of the object pointed to by dest.

    Your int takes several bytes, e.g. a 32bit int will be filled with 1,1,1,1 (in base 256, endianess doesn't matter in this case), which you then falsly interpreted as a "garbage" value.

提交回复
热议问题