Search code examples
c++sdl-2

Why does SDL_RWFromConstMem() size need type int instead of unsigned int?


Why does SDL_RWFromConstMem() size need type int instead of unsigned int?

I have a std::vector that hold data. vector.size() cannot return a value is less than 0.

Is that possible data's size is less than 0?


Solution

  • Even if a value can't be negative, it doesn't necessarily mean you should use an unsigned type for it. The main benefit they have is the ability to store larger positive values, but the downside is that some arithmetic operators behave unintuitively on them. Because of this, some programmers try to avoid unsigned types altogether when possible.

    The real problem isn't that it's not unsigned int, the problem is that it's not 64 bit wide on 64-bit platforms, where you can realistically have memory blocks longer than 2gb or 4gb (as representable by 32-bit int and unsigned int respectively).

    The fix is to use size_t or its signed counterpart (ptrdiff_t).

    And it seems they did exactly that in SDL3: https://wiki.libsdl.org/SDL3/SDL_IOFromConstMem (which wasn't released yet).