ZeroMemory and memset Dangers in C + + should be used with caution

Source: Internet
Author: User

When programming with C + +, ZeroMemory, memset, or "={0}" are often used to initialize or zero a struct object. However, these three ways have their own characteristics, the use of caution, or prone to serious errors, I today to solve a cause of the crash, check a few hours, only to find that the zeromemory caused by a colleague disorderly use. So collect information, essays this article to encourage each other.

Memset

void *memset (void *s,int ch,size_t N); is a provided function provided by the C Run-time Library to populate a block of memory with a given value, which is the quickest way to clear 0 operations on a larger struct or array. It is available on a language level, so it can be used across platforms. Reference: http://www.cplusplus.com/reference/cstring/memset/
Example:

[CPP]View PlainCopy
    1. Char str[] = "Almost every programmer should know memset!";
    2. memset (str,'-', 6);
    3. Puts (str);

Output:

[CPP]View PlainCopy
    1. ------Every programmer should know memset!
ZeroMemory

ZeroMemory is a macro in the Software Development Kit SDK of the United States Microsoft Corporation. The function is to fill a memory area with the zeros. The definition is as follows

[CPP]View PlainCopy
    1. #define RtlZeroMemory (Destination,length) memset ((Destination), 0, (Length))
    2. #define ZeroMemory RtlZeroMemory

Thus:

    1. ZeroMemory is actually implemented with Memset.
    2. ZeroMemory can only be used on Windows platforms.

Attention:

ZeroMemory and Memset and at zero, all bytes in the structure will be set to 0, if there is a virtual function in the struct or struct member has a virtual function, then the virtual function pointer will be 0, if the subsequent program calls virtual function, the null pointer is likely to cause the program to crash!

Therefore, there is a virtual function or a member of the structure of the virtual function initialization, it must be done with a constructor.

In addition, if the structure of a class contains STL templates (vectors, lists, maps, and so on), using ZeroMemory to clear 0 of objects in this class can also cause a series of crash problems (pointers to memory errors, iterators out-of-bounds access, and so on). Therefore, it is strongly recommended again: class is only initialized with constructors, do not call ZeroMemory to clear 0 operations.

Example:

[CPP]View PlainCopy
  1. #include "stdafx.h"
  2. #include <Windows.h>
  3. Class Car
  4. {
  5. Public
  6. virtual void Run () {}
  7. Private
  8. int m_speed;
  9. };
  10. struct Srace
  11. {
  12. Car car;
  13. int dirver;
  14. };
  15. int _tmain (int argc, _tchar* argv[])
  16. {
  17. Srace race;
  18. ZeroMemory (&race,sizeof (race));
  19. //Not through the virtual table pointer call, nothing
  20. Race.car.Run ();
  21. Car *pcar = &race.car;
  22. //__vfptr = 0x00000000, Crash
  23. Pcar->run ();
  24. return 0;
  25. }
={0}

The ={0} operation is an initialization of structs and arrays that assigns a default value to the underlying type variable in the struct, and compiles an error when there are non-basic types (such as class objects) in the struct, which is also a protection.

ZeroMemory and memset Dangers in C + + should be used with caution

Related Article

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.