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

前端 未结 7 873
南方客
南方客 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 01:52

    I always use:

    STRUCT theStruct = {}; // for C++, in C use {0}
    

    It's shorter, standard, therefore more elegant, and I don't really care about the theoretical differences. We are talking about code for a concrete OS here.

    Another advantage is you can also immediately set the struct size in the first member like this:

    STRUCT theStruct = {sizeof(STRUCT)}; 
    

    Many Win32 structs require you to set the size in a first member.

提交回复
热议问题