Search code examples
javac++bufferlimitbytebuffer

How would you limit a buffer size and also return the buffer limit in c++?


I know that in Java you use Bytebuffer .limit() but I'm not sure how to do it in c++. Thanks! ☺

Here is part of the code:

float _proj[16];
float _projb[1];

//this->_proj.flip().limit(16);
swap(_proj, _projb);
this->_proj.get(this->proj);

I used std::swap method instead of flip since there ins't ByteBuffer in c++.


Solution

  • Some close alternatives are std::vector and std::array.

    If you want to use an array of bytes, usually uint8_t, you will need to pass the array to functions, as well as the capacity and the number of elements in the array.

    The issue is that when arrays are passed to functions, the array decays down to a pointer to the first element, without any capacity information.

    There are no facilities to prevent you from indexing beyond the limits (capacity) of the array. This is why std::vector is a safer choice.