> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# XMemAlloc

> XMemAlloc 是 XBOX GDK 上由應用程式定義的記憶體配置回呼，可取代 CRT 和系統堆積的預設配置器。

# XMemAlloc

代表由應用程式定義、用來取代預設記憶體配置器的記憶體配置回呼函式。

<a id="syntaxSection" />

## 語法

```cpp theme={null}
PVOID XMemAlloc(  
         SIZE_T Size,  
         ULONGLONG Attributes  
)  
```

<a id="parametersSection" />

### 參數

*Size*   \_In\_<br />類型：SIZE\_T

所要求之記憶體區塊的大小 (以位元組為單位)。

*Attributes*   \_In\_<br />類型：ULONGLONG

記憶體配置的屬性。如需詳細資訊，請參閱 [XALLOC\_ATTRIBUTES](/zh-TW/reference/system/xmem/structs/xalloc_attributes)。

<a id="retvalSection" />

### 傳回值

類型：PVOID

如果成功，則為已配置記憶體的指標；否則為 `NULL`。

<a id="remarksSection" />

## 備註

您可以實作此函式以提供應用程式專屬的記憶體配置。此函式的定義必須符合 [XMEMALLOC\_ROUTINE](/zh-TW/reference/system/xmem/functions/xmemalloc_routine) 函式的定義。同樣地，您也可以實作符合 [XMEMFREE\_ROUTINE](/zh-TW/reference/system/xmem/functions/xmemfree_routine) 函式定義的 [XMemFree](/zh-TW/reference/system/xmem/functions/xmemfree) 函式，以提供應用程式專屬的記憶體解除配置。

實作後，您便可以使用 [XMemSetAllocationHooks](/zh-TW/reference/system/xmem/functions/xmemsetallocationhooks) 函式，以您自己的應用程式專屬 `XMemAlloc` 和 `XMemFree` 函式取代預設的記憶體配置和解除配置函式。如需 `XMemAlloc` 的詳細資訊，請參閱 [GDK 的圖形記憶體概觀](/zh-TW/build/core-features/graphics/graphics-memory-overview)。

<Note>
  **XMemAlloc** 預設實作的原始程式碼會隨 Microsoft Game Development Kit (GDK) 一起安裝。如需詳細資訊，請參閱 `\GXDK\gameKit\Source\amd64\heap.c` 中的 `XMemAllocDefault` 函式。
</Note>

<Note>
  如果您實作自己的 **XMemAlloc** 函式，極為重要的是不要從中呼叫 Direct3D 12 驅動程式。這是因為驅動程式的正常運作可能會導致叫用 **XMemAlloc**，而且沒有防止遞迴的保護。此需求也延伸至不得對 Direct3D 12 物件執行最終的 Release 作業，因為這可能會導致隱含的驅動程式呼叫。
</Note>

<a id="requirementsSection" />

## 需求

**標頭：** xmem.h

**程式庫：** xmem.lib

**支援的平台：** XBOX One 系列主機和 XBOX Series 主機

<a id="seealsoSection" />

## 概念文件

* [什麼是 Microsoft Game Development Kit？](https://learn.microsoft.com/gaming/gdk/docs/gdk-dev/intro/introduction)

## 另請參閱

[診斷與記憶體追蹤](/zh-TW/build/console-features/memory/system-memory-diagnostics)<br />[XMem](/zh-TW/reference/system/xmem/xmem_members)<br />[XMemAllocDefault](/zh-TW/reference/system/xmem/functions/xmemallocdefault)


## Related topics

- [Xmemalloc routine](/ko/reference/system/xmem/functions/xmemalloc_routine.md)
- [XMEMALLOC_ROUTINE callback](/reference/system/xmem/functions/xmemalloc_routine.md)
- [XMEM_ALLOCATION_HOOKS](/reference/system/xmem/structs/xmem_allocation_hooks.md)
