Any reason to prefer memset/ZeroMemory to value initialization for WinAPI structs?

前端 未结 7 840
南方客
南方客 2020-12-31 01:42

In Win32 programming a handful of POD structs is used. Those structs often need to be zeroed out before usage.

This can be done by calling memset()/

7条回答
  •  天涯浪人
    2020-12-31 02:16

    The end result is identical (so long as you assume that 0 is always represented by all-zero-bits), so it's largely a matter of style. Personally, I prefer value initialisation, since it is simpler and doesn't require function calls.

    Incidentally, you must initialise at least one member:

    STRUCT theStruct = {0};
    

    Omitting the 0 is allowed by some C compilers, but not by the C standard. C++ allows the omission, but I prefer to just always use the 0.

提交回复
热议问题