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
ZeroMemory
and such are part of the windows API itself. memset
is part of the C standard library.
For typical userland code, I'd normally use memset
(or the equivalent provided by your language of choice). If you're writing kernel code (e.g., a device driver) using something like ZeroMemory
is more attractive. Since your code executes in kernel mode anyway, you don't incur the cost of a task switch to use it. Since it's already in the Windows code, you aren't carrying around extra code in your driver to duplicate what's already there. At the same time, you do incur the cost of a function call, and in the case or zeroing (especially a small block of) memory, inline code may be significantly faster, and a rep stosd
doesn't take much code (in fact, setting up and using rep stosd
may take less code that a function call).
Because the Windows API should be language-agnostic. It provides sufficient functionality for developers, regardless of the language they use. Of course, eventually many functions will duplicate existing functionality offered by the languages.
You should call the winapi functions (and macros) directly whenever you need a certain level of control -- compare fopen()
with CreateFile()
for instance. Otherwise, prefer language-specific constructs over API calls. At least, you gain more platform-independence.