Why does ZeroMemory()
, and similar calls exist in the Windows API when there are memset and related calls in the C standard library already? Which ones should I call? I can guess the answer is "depends". On what?
Use ZeroMemory to clear a block of memory in any programming language. This macro is defined as the RtlZeroMemory macro.
We aren't going to dig into the assembly for memset here, but the fastest possible memset would run at 32 bytes/cycle, limited by 1 store/cycle and maximum vector the width of 32 bytes on my machine, so the measured value of 29 bytes/cycle indicates it's using an implementation something along those lines.
In C and C++, ZeroMemory()
and memset()
are the exact same thing.
/* In winnt.h */ #define RtlZeroMemory(Destination,Length) memset((Destination),0,(Length)) /* In winbase.h */ #define ZeroMemory RtlZeroMemory
Why use ZeroMemory()
then? To make it obvious. But I prefer memset()
in C or C++ programs.
The actual reason is that on a different platform it might be implemented in a more efficient way than memset
. Don't forget that Windows NT was designed as a highly portable operating system, it actually ran on Alpha, MIPS and Power PC. So, if the fooPC platform came out and has some assembly way to ultra-fast set memory to zero, it can be implemented without changing the high level API. This is no longer true for Windows, since now it only supports x86 and amd64 platforms, however it is still true for Windows CE.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With