Note: This article corresponds to the Windows programming environment.
In short: Generally, ZeroMemory is used for memory block initialization. Use SecurZeroMemory before destroying memory blocks that store sensitive data or releasing memory blocks that contain sensitive data (such as passwords and keys. If "= {0}" is not used for any special reason }".
Using the memset function to set the memory block to 0 is completely correct. Memset is easier to use across platforms, but C/C ++ is a nightmare. Note that when using memset, W. richard Stevens mentioned in UNIX network programming that the last two parameters of void * memset (void * dest, int c, size_t count) are easy to be reversed and cannot be found during compilation.
ZeroMemory macro is implemented by memset at the underlying layer. Only ZeroMemory is easier and more robust. Or it looks more cool and professional. ZeroMemory is recommended for programs on the Microsoft platform.
The SecurZeroMemory function can be seen as an enhanced security version of ZeroMemory. Do you notice that ZeroMemory is a macro and SecurZeroMemory is a function? Under certain compilation and optimization conditions, if ZeroMemory is used to set the memory block after 0 is no longer referenced, ZeroMemory may be "optimized" and not executed. If the memory stores sensitive information such as the user's password and encryption/decryption algorithm key, hackers may gain a peek. SecurZeroMemory will not be "optimized" under any conditions. Therefore, SecurZeroMemory should be used before the memory block is destroyed to store sensitive data or the memory block with sensitive data is released, instead of ZeroMemory.
As for the "= {0}" format, try not to use it. It is not intuitive enough. There are also some problems in memory alignment. If you are interested, please refer to Raymond Chen's Why do Microsoft code samples tend to use ZeroMemory instead of {0}?
By The way, Raymond Chen is not a common character. He is a veteran of The Windows Group and is The author of The old new thing, if you are interested in the technical history and principles of Windows, you can read it. His Blog: http://blogs.msdn.com/ B /oldnewthing/ is still very active.
============================== The End ========== ======================================