Hi,
I tried two different Linux/ARM cross-compiler on the following code ( see the last part of this message), one is from ELDK
Below is the running output on an ARM920T board:
before: p1:
0xfc 0x01 0x12 before: p2: 0x40 0x19 0x21 after: p1: 0x40 0x01 0x02 after: p2: 0x40 0x19 0x21 !!! cp is wrongThat is, the line 38 which copy 3 bytes, starting from p2, to p1, but the immediately followed memcmp(p1, p2, 3) failed. I am sure the memcpy did not do the job, since if I provide my own memcpy implemention as below, the error will go disappear.
void memcpy(void *dest, void *src, size_t n) { uint8_t *d = (uint8_t*)dest; uint8_t *s = (uint8_t*)src;
for (size_t i = 0; i < n; ++i) *d++ = *s++; }
Another way to make the error going away is not to use any optimization when compile. My test case use -O2 to bring up the error, but If I don't use it, the error won't occur.
I can not understand what happened here, the result shows, as you see, my assignment of p2 (line 34) is okay because the print output of line
36 is correct. And, if that is a bug in runtime c library -- remember my own memcpy works -- why the two cross compiler coming from different sources result in the same error?------------------------------------- the minimum sample
--------------------------------------------------
1 #include 2 #include 3 #include 4 #include 5 6 struct Foo { 7 uint8_t x; 8 uint8_t y; 9 uint8_t z; 10 uint8_t m[3]; 11 }; 12 13 struct Bar 14 { 15 uint8_t m[3]; 16 }; 17 18 void pr(const char *title, const void *block, size_t n) 19 { 20 printf("%s\n", title); 21 22 uint8_t *p = (uint8_t*)block; 23 for (size_t i = 0; i < n; ++i) 24 printf("0x%02x ", *p++); 25 26 printf("\n"); 27 } 28 29 void cp(const Foo *foo) 30 { 31 Bar bar; 32 33 Bar *p1 = &bar; 34 Bar *p2 = (Bar*)(foo->m); 35 pr("before: p1:", p1, 3); 36 pr("before: p2:", p2, 3); 37 38 memcpy(p1, p2, 3); 39 pr("after: p1:", p1, 3); 40 pr("after: p2:", p2, 3); 41 42 if (memcmp(p1, p2, 3) != 0) 43 printf("!!! cp is wrong\n"); 44 } 45 46 int main() 47 { 48 Foo foo; 49 foo.x = 1; 50 foo.y = 2; 51 foo.z = 3; 52 foo.m[0] = 0x40; 53 foo.m[1] = 0x19; 54 foo.m[2] = 0x21; 55 56 cp(&foo); 57 cp2(&foo); 58 return 0; 59 }---------------------------------------------------------------------------------------