Is it guaranteed that memset will zero out the padding bits in a structure?

后端 未结 3 2307
独厮守ぢ
独厮守ぢ 2021-02-20 16:54

In general ,as per C standard is it guaranteed that memset() with 0 will zero out the padding bits in a C structure?

What about gcc?

For example , something lik

3条回答
  •  无人及你
    2021-02-20 17:27

    Yes, memset writes a 32 bit value into a contiguous region of memory of a given length starting at the given address. In your case, memset writes the region with (32bit value) 0.

    So if you do a memset of length sizeof (your_struct), you should be fine:

    memset(&ms, 0, sizeof(struct MyStruct));
    

提交回复
热议问题