Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do ZeroMemory, etc. exist when there are memset, etc. already?

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?

like image 766
CannibalSmith Avatar asked Jun 14 '10 15:06

CannibalSmith


People also ask

What does ZeroMemory do in C++?

Use ZeroMemory to clear a block of memory in any programming language. This macro is defined as the RtlZeroMemory macro.

How fast is memset?

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.


2 Answers

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.

like image 128
In silico Avatar answered Sep 20 '22 14:09

In silico


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.

like image 24
lornova Avatar answered Sep 21 '22 14:09

lornova