Archive 17/01/2023.

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

graveman82

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)”?

Modanung

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:

graveman82

I am too lazy to build new version

Modanung

Can’t help with that. :slightly_smiling_face:

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

Eugene

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

1vanK

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

graveman82

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

graveman82

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