Difference between ZeroMemory and memset

Started by
4 comments, last by LessBread 13 years, 5 months ago
Hi.

Let's say we have a window class of the type WNDCLASSEX named wc, is there any difference between writing:
memset(&wc, 0, sizeof(wc));
and
ZeroMemory(&wc, sizeof(wc));
Advertisement
ZeroMemory() is only available on Windows. Not a big deal, because you are using WinAPI anyway.
From whatever version of the Windows SDK is sitting on my computer:

Line 106 of WinBase.h:

#define ZeroMemory RtlZeroMemory

Line 13058 of WinNT.h:

#define RtlZeroMemory(Destination,Length) memset((Destination),0,(Length))
ZeroMemory has its right to exist because it is guaranteed not to be optimized out and moved and whatnot, or so I've read once. It's all bull, if you ask me :-)

memset works the same, and WNDCLASSEX wc = { sizeof(WNDCLASSEX) }; is even more concise.
The reason I heard it exists is because the Windows API code samples, while written primarily in C, were expected to be used as code examples for people using other programming languages. So rather than expecting a Pascal programmer to know that the {} initializer zeros values or what the C standard library function memset() does, they introduced a function that clearly describes what the intended effect is.
Quote:Original post by samoth
ZeroMemory has its right to exist because it is guaranteed not to be optimized out and moved and whatnot, or so I've read once.


That's SecureZeroMemory.
RtlZeroMemory and memset are both exported by ntdll.dll, at least on the version of ntdll.dll on my machine. Perhaps the one wraps a call to the other as with the macro.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man

This topic is closed to new replies.

Advertisement