Share via


ZeroDeviceMemory 函式

ZeroDeviceMemory 函式會將緩衝區的內容設定為零,而不會干擾編譯程式優化的情況,在開發人員需要另外確定存取裝置記憶體時不會產生對齊錯誤。

重要

某些資訊與發行前版本產品有關,在發行前版本產品可能經過大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。

參數

Param 目的地 [out]

記憶體區塊起始位址的指標,以零填滿。

Param Length [in]

要填滿零的記憶體區塊大小,以位元組為單位。

語法

volatile void*
  ZeroDeviceMemory (
    _Out_writes_bytes_all_(Length) volatile void* Destination,
    SIZE_T Length
  );

備註

此 API 是 FillDeviceMemory 的 便利包裝函式。 如需詳細資訊,請參閱 FillDeviceMemory備註。

注意

此函式適用於所有版本的 Windows,而不只是最新版本。 您必須取用最新的 SDK,才能從 winbase.h 標頭取得函式宣告。 您也需要來自最新 SDK 的連結庫 (volatileaccessu.lib)。 不過,產生的二進位檔將會在舊版 Windows 上正常執行。

範例

// In this scenario we are setting data on memory mapped
// as "device memory" (i.e. memory not backed by RAM) to the value zero. On
// some platforms like ARM64, device memory cannot tolerate
// memory accesses that are not naturally aligned (i.e. a 4-byte
// load must be 4-byte aligned). Functions like memset, FillMemory,
// and even FillVolatileMemory may perform unaligned memory accesses
// because it is typically faster to do this.
// To ensure only naturally aligned accesses happen, use FillDeviceMemory.
//
// ZeroDeviceMemory is an wrapper around FillDeviceMemory that sets the memory
// to zero.

ZeroDeviceMemory(DeviceMemoryBuffer, 100);

需求

最低支援的用戶端: Windows 11 Insider Preview 組建 TBD

標頭: winbase.h (包括 Winbase.h)

核心模式連結庫: volatileaccessk.lib

使用者模式連結庫: volatileaccessu.lib

另請參閱