# Malloc

> Allocates a block of memory of a specified size and alignment.

## Definition

* **Type:** Method
* **Namespace:** [Unity.Collections.LowLevel.Unsafe](/engine/6000.3/script-reference/unity/collections/lowlevel/unsafe.md)
* **Assembly:** UnityEngine.CoreModule

## Malloc(long, int, Allocator)

Allocates a block of memory of a specified size and alignment.

```csharp
public static void* Malloc(long size, int alignment, Allocator allocator)
```

### Parameters

**** (\[long]\(https\://learn.microsoft.com/dotnet/api/system.int64)): The number of bytes to allocate. Ensure this size matches the memory required for your data.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): The alignment for the allocated memory block.**** (\[Allocator]\(/engine/6000.3/script-reference/unity/collections/allocator)): The allocator type, such as Allocator.Temp or Allocator.Persistent, indicating how the memory is managed.

### Returns

| Type                                                         | Description                                                                                                                    |
| ------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------ |
| [void\*](https://learn.microsoft.com/dotnet/api/system.void) | A pointer to the allocated memory block. Manage this pointer carefully to prevent memory leaks and ensure proper deallocation. |

### Remarks

The `Malloc` method allocates a block of unmanaged memory. It allows developers to specify the size in bytes and the alignment of the memory block. This method is critical in performance-critical applications where precise memory control is required.

The memory allocated is not initialized to zero. Ensure that you free the allocated memory with [UnsafeUtility.Free](/engine/6000.3/script-reference/unity/collections/lowlevel/unsafe/unsafeutility/free.md) when it is no longer needed.

```csharp
using UnityEngine;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;

public class MallocExample : MonoBehaviour
{
    void Start()
    {
        // Specify the number of elements
        int numElements = 10;

        // Allocate memory for an array of integers
        unsafe
        {
            int* array = (int*)UnsafeUtility.Malloc(numElements * sizeof(int), UnsafeUtility.AlignOf<int>(), Allocator.Temp);

            // Initialize the array with some values
            for (int i = 0; i < numElements; i++)
            {
                array[i] = i * 2;
            }

            // Output the contents of the array
            for (int i = 0; i < numElements; i++)
            {
                Debug.Log(array[i]);  // Expected output: 0, 2, 4, 6, ..., 18
            }

            // Free the allocated memory
            UnsafeUtility.Free(array, Allocator.Temp);
        }
    }
}
```

Additional Resources: [UnsafeUtility.MallocTracked](/engine/6000.3/script-reference/unity/collections/lowlevel/unsafe/unsafeutility/malloctracked.md), [UnsafeUtility.FreeTracked](/engine/6000.3/script-reference/unity/collections/lowlevel/unsafe/unsafeutility/freetracked.md).

## Malloc(long, int, MemoryLabel)

Allocates a block of memory of a specified size and alignment, using the specified memory label.

```csharp
public static void* Malloc(long size, int alignment, MemoryLabel label)
```

### Parameters

**** (\[long]\(https\://learn.microsoft.com/dotnet/api/system.int64)): The number of bytes to allocate. Ensure this size matches the memory required for your data.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): The alignment for the allocated memory block.**** (\[MemoryLabel]\(/engine/6000.3/script-reference/unity/collections/memorylabel)): The memory label to allocate under.

### Returns

| Type                                                         | Description                                                                                                                    |
| ------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------ |
| [void\*](https://learn.microsoft.com/dotnet/api/system.void) | A pointer to the allocated memory block. Manage this pointer carefully to prevent memory leaks and ensure proper deallocation. |

### Remarks

The `Malloc` method allocates a block of unmanaged memory. It allows developers to specify the size in bytes and the alignment of the memory block. This method is critical in performance-critical applications where precise memory control is required.

Specifying a memory label helps in profiling and debugging memory usage in Unity.

The memory allocated is not initialized to zero. Ensure that you free the allocated memory with [UnsafeUtility.Free](/engine/6000.3/script-reference/unity/collections/lowlevel/unsafe/unsafeutility/free.md) when it is no longer needed.

Additional Resources: [MemoryLabel](/engine/6000.3/script-reference/unity/collections/memorylabel.md) [UnsafeUtility.MallocTracked](/engine/6000.3/script-reference/unity/collections/lowlevel/unsafe/unsafeutility/malloctracked.md), [UnsafeUtility.FreeTracked](/engine/6000.3/script-reference/unity/collections/lowlevel/unsafe/unsafeutility/freetracked.md).

### Examples

```csharp
using UnityEngine;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;

public class MallocMemoryLabelExample : MonoBehaviour
{
    static readonly MemoryLabel myMemoryLabel = new MemoryLabel("MyArea", "MyObject");

    void Start()
    {
        // Specify the number of elements
        int numElements = 10;

        // Allocate memory for an array of integers using label
        unsafe
        {
            int* array = (int*)UnsafeUtility.Malloc(numElements * sizeof(int), UnsafeUtility.AlignOf<int>(), myMemoryLabel);

            // Initialize the array with some values
            for (int i = 0; i < numElements; i++)
            {
                array[i] = i * 2;
            }

            // Output the contents of the array
            for (int i = 0; i < numElements; i++)
            {
                Debug.Log(array[i]);  // Expected output: 0, 2, 4, 6, ..., 18
            }

            // Free the allocated memory using label
            UnsafeUtility.Free(array, myMemoryLabel);
        }
    }
}
```
