Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
CloseFor some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
Closeptr1 | A pointer to the first memory buffer. This buffer contains the first block of data to compare. |
ptr2 | A pointer to the second memory buffer to compare against the first. Ensure this buffer is the same size as the first for an accurate comparison. |
size | The number of bytes to compare between the two memory regions. |
int Returns 0 if the contents of the two memory regions are identical; returns a non-zero value if they differ.
Checks whether two memory regions are identical.
The MemCmp
method compares two memory regions to determine if they contain identical data. It returns zero if the memory contents are the same and a non-zero value if they differ. This method can be used to compare data blocks efficiently in performance-critical applications.
using UnityEngine; using Unity.Collections.LowLevel.Unsafe;
public class MemCmpExample : MonoBehaviour { void Start() { int numElements = 5; int size = numElements * sizeof(int); unsafe { // Use stackalloc to allocate memory for three buffers int* buffer1 = stackalloc int[numElements]; int* buffer2 = stackalloc int[numElements]; int* buffer3 = stackalloc int[numElements];
// Initialize buffer1 with data for (int i = 0; i < numElements; i++) { buffer1[i] = i * 10; // 0, 10, 20, 30, 40 }
// Copy contents from buffer1 to buffer2 UnsafeUtility.MemCpy(buffer2, buffer1, size);
// Modify buffer3 differently for (int i = 0; i < numElements; i++) { buffer3[i] = buffer1[i] + 1; // 1, 11, 21, 31, 41 }
// Compare buffer1 and buffer2 int result1 = UnsafeUtility.MemCmp(buffer1, buffer2, size); Debug.Log($"Comparing buffer1 and buffer2, Result: {result1}"); // Expected output: 0 (identical)
// Compare buffer1 and buffer3 int result2 = UnsafeUtility.MemCmp(buffer1, buffer3, size); Debug.Log($"Comparing buffer1 and buffer3, Result: {result2}"); // Expected output: non-zero (different) } } }
Additional resources: UnsafeUtility.MemSet.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.