Three methods for setting the memory block to 0 in C ++: memset, ZeroMemory, and SecurZeroMemory

Source: Internet
Author: User

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 ========== ======================================

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.