• Advertisement
Sign in to follow this  

Difference between ZeroMemory and memset

This topic is 2628 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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));

Share this post


Link to post
Share on other sites
Advertisement
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))

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement