I compile this code on godbolt.org with -O2 and compilers don't optimize it using some memcpy, honestly running the loop.
void foo(int* dst, int* src, int n)
{
for (int i = 0; i < n; ++i)
{
dst[i] = src[i];
}
}
But if I replace "= src[i]" with "= 0" they use memset though. But again, when I replace it with " = 1", they run a loop. Why do they avoid memcpy and memset when the value to be set is not zero? I thought that's one of the first optimizations they will perform.