C++ 既然已经有了 memset 等,为什么还要存在 ZeroMemory 等呢?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3038302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do ZeroMemory, etc. exist when there are memset, etc. already?
提问by CannibalSmith
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?
ZeroMemory()
当 C 标准库中已经存在 memset 和相关调用时,为什么Windows API 中还存在 , 和类似的调用?我应该打电话给哪些?我可以猜到答案是“取决于”。什么?
回答by In silico
In C and C++, ZeroMemory()
and memset()
are the exact same thing.
在 C 和 C++ 中,ZeroMemory()
和memset()
是完全一样的东西。
/* 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.
那为什么要用ZeroMemory()
呢?使其显而易见。但我更喜欢memset()
C 或 C++ 程序。
回答by lornova
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.
实际原因是,在不同的平台上,它可能以比memset
. 不要忘记 Windows NT 被设计为高度便携的操作系统,它实际上可以在 Alpha、MIPS 和 Power PC 上运行。因此,如果 fooPC 平台出来并且有一些组装方式可以将内存超快设置为零,则可以在不更改高级 API 的情况下实现。这不再适用于 Windows,因为现在它只支持 x86 和 amd64 平台,但它仍然适用于 Windows CE。
回答by Jerry Coffin
ZeroMemory
and such are part of the windows API itself. memset
is part of the C standard library.
ZeroMemory
这些是 Windows API 本身的一部分。memset
是 C 标准库的一部分。
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).
对于典型的用户空间代码,我通常会使用memset
(或您选择的语言提供的等效代码)。如果您正在编写内核代码(例如,设备驱动程序),则使用类似的ZeroMemory
方法更有吸引力。由于您的代码无论如何都是在内核模式下执行的,因此您不会产生使用它的任务切换成本。由于它已经存在于 Windows 代码中,因此您无需在驱动程序中携带额外的代码来复制已有的代码。同时,您确实会产生函数调用的成本,并且在将(尤其是一小块)内存归零的情况下,内联代码可能会明显更快,并且rep stosd
不需要太多代码(实际上,设置up 和 usingrep stosd
可能需要比函数调用更少的代码)。
回答by Humberto
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.
因为 Windows API 应该是语言不可知的。它为开发人员提供了足够的功能,无论他们使用何种语言。当然,最终许多功能将复制这些语言提供的现有功能。
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.
你应该调用WINAPI函数(宏直接)时,你需要控制在一定水平-比较fopen()
有CreateFile()
例如。否则,比 API 调用更喜欢特定于语言的构造。至少,您获得了更多的平台独立性。
回答by Pawe? So?tysiak
Because, ZeroMemory don't require line of comment
因为,ZeroMemory 不需要注释行
回答by richard.albury
Actually, what you wantto use is SecureZeroMemory()
.
实际上,您要使用的是SecureZeroMemory()
.
An optimizing compiler can remove calls to memset()
, and SecureZeroMemory()
is designed to prevent this.
优化编译器可以删除对 的调用memset()
,SecureZeroMemory()
旨在防止这种情况发生。
I used to think the ZeroMemory()
calls were unnecessary until I came across this fact.
ZeroMemory()
在我发现这个事实之前,我曾经认为这些电话是不必要的。
回答by Andreas Rejbrand
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,
我认为有一点是所有 Win32 项目中的内存分配函数应该看起来相同,独立于编程语言。事实上,正如之前所指出的,在 C 中,ZeroMemory 实际上是 memset,C 函数。在德尔福,
procedure ZeroMemory(Destination: Pointer; Length: DWORD);
begin
FillChar(Destination^, Length, 0);
end;
where FillChar is the Delphi function. And so on:
其中 FillChar 是 Delphi 函数。等等:
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;
...