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
- Char str[] = "Almost every programmer should know memset!";
- memset (str,'-', 6);
- Puts (str);
Output:
[CPP]View PlainCopy
- ------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
- #define RtlZeroMemory (Destination,length) memset ((Destination), 0, (Length))
- #define ZeroMemory RtlZeroMemory
Thus:
- ZeroMemory is actually implemented with Memset.
- 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
- #include "stdafx.h"
- #include <Windows.h>
- Class Car
- {
- Public
- virtual void Run () {}
- Private
- int m_speed;
- };
- struct Srace
- {
- Car car;
- int dirver;
- };
- int _tmain (int argc, _tchar* argv[])
- {
- Srace race;
- ZeroMemory (&race,sizeof (race));
- //Not through the virtual table pointer call, nothing
- Race.car.Run ();
- Car *pcar = &race.car;
- //__vfptr = 0x00000000, Crash
- Pcar->run ();
- return 0;
- }
={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