# MemCpyReplicate(void*, void*, int, int)

> Copies memory from a source to a destination and replicates it multiple times.

## Definition

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

```csharp
public static void MemCpyReplicate(void* destination, void* source, int size, int count)
```

### Parameters

**** (\[void\*]\(https\://learn.microsoft.com/dotnet/api/system.void)): A pointer to the destination memory block. Ensure this block has enough space to hold the replicated data.**** (\[void\*]\(https\://learn.microsoft.com/dotnet/api/system.void)): A pointer to the source memory block. This block should contain the data you want to replicate.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): The size, in bytes, of the data block to copy and replicate. This specifies the number of bytes that are replicated each time.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): The number of times to replicate the source data block in the destination buffer.

### Remarks

The `MemCpyReplicate` method copies a specified block of memory from a source location and replicates that block multiple times into a destination location. This is useful for quickly initializing a large buffer with repeated values, such as setting up constant buffers or initializing large arrays with repeated patterns.

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

public class MemCpyReplicateExample : MonoBehaviour
{
    void Start()
    {
        int elementCount = 3;
        int replicateCount = 4;
        int dataSize = elementCount * sizeof(float);

        // Use stackalloc to allocate memory for source and destination buffers
        unsafe
        {
            float* srcBuffer = stackalloc float[elementCount];
            float* dstBuffer = stackalloc float[elementCount * replicateCount];

            // Initialize the source buffer with multiple values
            srcBuffer[0] = 1.1f;
            srcBuffer[1] = 2.2f;
            srcBuffer[2] = 3.3f;

            // Use MemCpyReplicate to copy and replicate the source values into the destination
            UnsafeUtility.MemCpyReplicate(dstBuffer, srcBuffer, dataSize, replicateCount);

            // Output the contents of the destination buffer to verify replication
            Debug.Log("Contents of destination buffer after MemCpyReplicate:");
            for (int i = 0; i < elementCount * replicateCount; i++)
            {
                Debug.Log(dstBuffer[i]);  // Expected pattern: 1.1, 2.2, 3.3 repeated
            }
        }
    }
}
```

Additional Resources: [UnsafeUtility.MemCpy](/engine/6000.6/script-reference/unity/collections/lowlevel/unsafe/unsafeutility/memcpy.md).
