EDIT
I found the memcpy
definition has told us about its undefined behavior when target
and src
has overlap:
The memcpy() function copies n bytes from memory area src to memory area dst. If dst and src overlap, behavior is undefined.Applications in which dst and src might overlap should use memmove(3) instead.
Original Question
I have a simple program that looks like this:
static void RotateLeft(bool *In, int len, int loop) {
for(int i = 0;i< 28;i++) {
LOGI("%d -> %d", i, In[i]);
}
bool Tmp[256] = {0};
memcpy(Tmp, In, loop);
memcpy(In, In + loop, len - loop);
LOGI("len = %d, loop = %d", len, loop); // <--- always 28 and 1
for(int i = 0;i< 28;i++) {
LOGI("%d -> %d", i, In[i]); <----- broken
}
memcpy(In + len - loop, Tmp, loop);
}
RotateLeft(`bool array`, 28, 1)
It is weird that this program doesn't work right on the arm64-v8a
platform (but works well on other platforms):
The input array is something like this:
0 0 0 1 1 1 1 0 1 0 0 1 0 1 ...
The rotated array should be:
0 0 1 1 1 1 0 1 0 0 1 0 1 0...
But it actually outputs:
0 0 1 1 1 1 0 1 0 0 1 1 0 0 ...
UPDATE
Here's how this program allocate array:
bool K[64], *KL=&K[0], *KR=&K[28];
// do something fill `K`
RotateLeft(KR, 28, 1);