c# - MemoryMappedFile.CreateNew(...) 是否保证创建一个带零的文件?

标签 c# zero memory-mapped-files

正如标题所说;我可以依赖 MemoryMappedFile 来创建一个全为零的文件吗?我已经通过读取一些新创建的文件进行了测试,但这种“测试”似乎是徒劳的,尽管我还没有看到非零字节。

是否有任何我遗漏的相关文档,或者只是假设内存映射文件在创建时为空。

最佳答案

不,这不能保证。这似乎是特定于实现的。

虽然在 MemoryMappedFile 中没有提到,它在 .NET 中使用的底层原生 API 的行为 (CreateFileMapping) 确实明确记录了这一点,并且它有所不同,具体取决于存储的来源。

在文件支持映射的情况下:

If the file is extended, the contents of the file between the old end of the file and the new end of the file are not guaranteed to be zero; the behavior is defined by the file system.

在匿名映射的情况下(由页面文件支持):

The initial contents of the pages in a file mapping object backed by the operating system paging file are 0 (zero).

关于c# - MemoryMappedFile.CreateNew(...) 是否保证创建一个带零的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30714650/

相关文章:

c++ - WinAPI:是否需要在可执行内存映射文件上调用 FlushInstructionCache?

c# - 如何从 .NET 对象创建 XML 文档?

c# - WPF Databinding DataTrigger 根据 bool 值改变形状的颜色

javascript - 添加到表单数据的 JavaScript 对象在服务器上始终接收为 null

sql - 如何使用 SQL Server 找到最接近零的值

linux - 如何 "zero"为 1 的文件

c#内存映射文件读取

c# - WebHostBuilder.Build() MissingMethodException in .NET Core 迁移方案

c++ - 如何在if条件下区分零和未定义?

linux - 将 DMA 缓冲区写入内存映射文件