You can use memset()
, which has a core of optimized assembly with SSE/SSE2/etc instructions as appropriate at runtime. The memset()
function is designed to do exactly this task: set each char
in an array of char
to a specific value, as quickly as possible.
#include <cstring>
char *dataPtr = new char[LENGTH];
std::memset(dataPtr, 200, LENGTH);
However, modern compilers will do this for you, you can check the assembly, and you might find a call to memset()
or something similar in the original code that used a for
loop. It's only 40 kB anyway, so you're not going to save much time, unless you have to initialize the array very often.