#RemoveRange should it be memcpy or memmove?

1 messages · Page 1 of 1 (latest)

dense steeple
#
public void RemoveRange(int index, int count)
{
    CheckIndexCount(index, count);

    index = CollectionHelper.AssumePositive(index);
    count = CollectionHelper.AssumePositive(count);

    if (count > 0)
    {
        int copyFrom = math.min(index + count, m_length);
        var sizeOf = sizeof(T);
        void* dst = (byte*)Ptr + index * sizeOf;
        void* src = (byte*)Ptr + copyFrom * sizeOf;
        UnsafeUtility.MemCpy(dst, src, (m_length - copyFrom) * sizeOf);
        m_length -= count;
    }
}```

This is UnsafeList RemoveRange implementation. I was always under the assumption that MemMove should be used for overlapping ranges or have I been misunderstanding this.
tepid tangle
#

if they match standard C semantics then it should definitely be memmove; in standard C memcpy on overlapping ranges is undefined behavior

#

which to be fair is allowed to support overlapping ranges, but is not required to do so

#

the math.min there also seems pointless because you can only get to it if count > 0 and if it then clamps the src to m_length you're reading count elements past the array bounds anyway