memset() or value initialization to zero out a struct?

后端 未结 8 2178
你的背包
你的背包 2020-11-27 10:29

In Win32 API programming it\'s typical to use C structs with multiple fields. Usually only a couple of them have meaningful values and all others have to be zer

8条回答
  •  眼角桃花
    2020-11-27 11:27

    If your struct contains things like :

    int a;
    char b;
    int c;
    

    Then bytes of padding will be inserted between "b" and "c". memset() will zero those, the other way will not, so there will be 3 bytes of garbage (if your ints are 32 bits). If you intend to use your struct to read/write from a file, this might be important.

提交回复
热议问题