Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to deal with medium-sized byte arrays in modern C++?

Tags:

c++

c++11

Many languages and frameworks offer a "byte array" type, but the C++ standard library does not. What type is appropriate to use for medium-sized1, resizable byte arrays and how can I use that type efficiently? (particularly: allocating, passing as parameters and destroying)


1: By medium-sized I mean less than a 100 MB.

like image 430
Tamás Szelei Avatar asked Nov 29 '25 05:11

Tamás Szelei


1 Answers

You can use std::vector<unsigned char>, or as @Oli suggested std::vector<uint8_t>. Yes, you can pass around it, without copying the whole contents.

void f(std::vector<unsigned char> & byteArray) //pass by reference : no copy!
{
     //
}

std::vector<unsigned char>  byteArray;
//...
f(byteArray); //no copying is being made!
like image 134
Nawaz Avatar answered Dec 01 '25 18:12

Nawaz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!