36

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?

1
  • One important reason would be integrating (unifying) the interface. We should also think about the history - not all programming plans go as planned. MS has a long history in this world so there are many historical reasons here and there in general. Generally, in the programming world, it is helpful to think about the intention at the time of planning(or designing) even if it has not actually become a reality. Sep 4, 2023 at 0:07

8 Answers 8

48

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.

3
  • 2
    It's a bigger issue, as I noted in my answer: optimizing compilers can remove calls to memset(), so you really want to use something which won't be optimized away. Feb 20, 2013 at 22:12
  • 6
    @richard.albury in that case you should use SecureZeroMemory, as ZeroMemory could be optimized away.
    – ya23
    Jul 31, 2015 at 12:28
  • 2
    @ya23 I hate to "Well, actually", but that's exactly what I said in my answer. ;-) Aug 8, 2019 at 12:35
12

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.

2
  • 1
    I don't think this is the reason. If an efficient way to zero memory exists, they could incorporate that into the memset implementation, too.
    – Maxpm
    Jun 18, 2018 at 19:56
  • @Maxpm in Windows there isn't a system provided C standard library. Each executable links (often statically) with the C library provided by its compiler.
    – lornova
    Jun 20, 2018 at 7:23
10

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

2
  • In newer Visual Studio compilers memset() is an intrinsic function. That means that it is implemented by compiler itself and in many cases there's no function call. Compiler inlines it with specially crafted assembly code to perform best based on target architecture, length and memory alignment of data being overwritten.
    – BJovke
    Jan 26, 2018 at 18:52
  • 1
    @BJovke: that's not a particularly recent innovation. It goes back to at least MS C 6.0 (note: MS C, not VC++), which (going for memory, so it could be a little wrong) was released around 1989 or '90. I guess I didn't say it directly in the answer, but that was the point of mentioning the overhead of a function call (i.e., that using ZeroMemory would involve a function call, but using memset often wouldn't). Jan 26, 2018 at 22:33
8

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.

5
  • 3
    I'm not sure how you would write a language-agnostic macro. Jun 14, 2010 at 15:15
  • ZeroMemory appears to be a macro for cross MS-language nomenclature. I checked as it was possibly a system trick for hardware memory clearing which could have been more efficient. It is probably just syntactic sugar for memset; ignore it. msdn.microsoft.com/en-us/library/aa366920(VS.85).aspx
    – msw
    Jun 14, 2010 at 15:16
  • 1
    @Pete: if you have ZeroMemory defined in the C stdlib and - say VBA - then your coder uses the same name for the same purpose and is better locked-in to Microsoft. Embrace and extend, brother! en.wikipedia.org/wiki/Embrace,_extend_and_extinguish
    – msw
    Jun 14, 2010 at 15:18
  • @Pete ouch! Macro or not, isn't it a part of the Windows API?
    – Humberto
    Jun 14, 2010 at 15:29
  • @Humberto It is part of the Windows API, but the Windows API is defined in C. Why do you believe the Windows API should be language agnostic? Jun 14, 2010 at 15:38
8

Actually, what you want to use is SecureZeroMemory().

An optimizing compiler can remove calls to memset(), and SecureZeroMemory() is designed to prevent this.

I used to think the ZeroMemory() calls were unnecessary until I came across this fact.

0
5

Because, ZeroMemory don't require line of comment

3

I think one point is that the memory allocation functions should look the same in all Win32 projects, independent of the programming language. Indeed, as have been pointed out before, in C, ZeroMemory is actually memset, the C function. In Delphi,

procedure ZeroMemory(Destination: Pointer; Length: DWORD);
begin
  FillChar(Destination^, Length, 0);
end;

where FillChar is the Delphi function. And so on:

procedure MoveMemory(Destination: Pointer; Source: Pointer; Length: DWORD);
begin
  Move(Source^, Destination^, Length);
end;

procedure FillMemory(Destination: Pointer; Length: DWORD; Fill: Byte);
begin
  FillChar(Destination^, Length, Fill);
end;

...
1
  • Really out of topic, but as a Delphi developer, I tend to use MoveMemory (or CopyMemory - they're identical) when I have pointers, and Move when I have variables, so I do not have to use "@" or "^". Jun 14, 2010 at 16:01
1

According to MSDN, ZeroMemory is a macro. It probably exists as a convenience (e.g., naming convention) or for backwards compatibility.

1
  • I'd go with the backwards compatibility, along with those functions for handling 32 bit numbers (I forget the names) for division etc. Jun 15, 2010 at 1:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.