Skip to content
Advertisement

Fast byte copy C++11

I need to convert C# app which uses extensively bytes manipulation.

An example:

JavaScript

Basically BitConverter and Buffer.BlockCopy called 100s times per sec.

There are several classes that inherit from the base class above doing more specific tasks. For example:

JavaScript

What approach in C++ should I look into?

Advertisement

Answer

Best option, in my opinion, is to actually go to C – use memcpy to copy over the bytes of any object.

Your above code would then be re-written as follows:

JavaScript

Here, I use uint8_t, uint16_t, and uint64_t as replacements for the byte, ushort, and ulong types.

Keep in mind, your timestamp copy is not portable to a big-endian CPU – it will cut off the lowest byte rather than the highest. Solving that would require copying in each byte manually, like so:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement