Poperton
Poperton

Reputation: 2127

Is it possible to splice buffers in C++ with zero copy?

Suppose I have two buffers:

uint8_t* buf1[100];
uint8_t* buf2[10];
uint8_t* buf3[90];

Where buf1 is full of data and I need to pass 10 bytes of this data to buf2 and the rest to buf3. Is there a way to do this without copying?

If not, is there a high level library (like std::vector) in which splicing is possible without copying?

Upvotes: 2

Views: 520

Answers (1)

L. F.
L. F.

Reputation: 20579

No, this is not possible. The three buffers are distinct objects with different addresses. Therefore, some form of copying is necessary.

You can use a span:

span<uint8_t*, 10> buf2(buf1, 10);
span<uint8_t*, 90> buf3(buf1 + 10, 90);

span is not in the standard library as of C++17, but it is available in the GSL. See What is a "span" and when should I use one?.

Upvotes: 6

Related Questions