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

前端 未结 7 846
南方客
南方客 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:04

    If your code will serve as an example to countless Visual Basic developers who are likely to not notice or understand the C++ = {} construct, ZeroMemory is a good way to make the C++ code look more like pseudocode and minimize the incidence of subtle, hair-ripping initialization bugs.

    That's the concern that the MSDN article authors faced, which then explains why ZeroMemory shows up in so much code (even C++ code).

    On the other hand, if the purpose of your C++ code is to make a working product, rather than to teach the world, using the elegant and expressive power of the C++ language is a great idea.

提交回复
热议问题