Bugs or strange places in code! -- Vector.h Vector::MoveRange(1)

Hi! I found some confusing place in Urho3D1.7 Vector.h:
> /// Move a range of elements within the vector.

void MoveRange(unsigned dest, unsigned src, unsigned count)
{
    T* buffer = Buffer();
    if (src < dest)
    {
        for (unsigned i = count - 1; i < count; --i) // THIS IS VERY STRANGE CODE
            buffer[dest + i] = buffer[src + i];
    }
    if (src > dest)
    {
        for (unsigned i = 0; i < count; ++i)
            buffer[dest + i] = buffer[src + i];
    }
}

shouldn’t it be “for (unsigned i = count - 1; i > 0; --i)”?

1.7 is getting old and this code has been reformulated in the meantime:

/// Move a range of elements within the vector.
void MoveRange(unsigned dest, unsigned src, unsigned count)
{
    if (count)
        memmove(Buffer() + dest, Buffer() + src, count * sizeof(T));
}

…and welcome to the forums! :confetti_ball: :slightly_smiling_face:

I am too lazy to build new version

Can’t help with that. :slightly_smiling_face:

Though the scripts included with the engine make it a breeze, once the dependencies are installed.

0 has to be included as well, so your code is incorrect.

usigned always >= 0, old code is correct because overflow occurs and the cycle ends

2 Likes

Ou, that’s very original approach, thank’s!

You are right, damn, I didn’t noticed that