Re: Library bug or my fault?
- From: Nick Keighley <nick_keighley_nospam@xxxxxxxxxxx>
- Date: Tue, 22 Jul 2008 01:29:32 -0700 (PDT)
On 22 Jul, 07:46, Steven Woody <narkewo...@xxxxxxxxx> wrote:
Please check the sample code listed in the end of the message. It was
compiled using ARM/Linux cross-compiler and run on an ARM9 target. I
think the problem applies to this group because two cross-compiler
from different vendor result same error. So I guess it is not vendor
specific. If my guess is right, then it means the code itself may get
problem, but I can not figure out where it is.
The problem is, the line 38 which copy 3 bytes, starting from p2, to
p1, but the immediately followed memcmp (line 42) shows the memcpy was
not well done. 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++;
}
Could you please check the code as well as the running result and
tell me what wrong with it? Thanks.
------------------------------------- the minimum sample
--------------------------------------------------
1 #include <stdio.h>
2 #include <string>
3 #include <stdint.h>
4 #include <assert.h>
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 }
------------------------------------------------------------------
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 wrong
post your code!!
You have no definition for cp2() so the code above won't compile.
I don't have a C99 compiler so I compiled this instead:
(can you miss out the "struct" keyword in C99?)
***************
#include <stdio.h>
/* #include <string> */ /* njk */
/* #include <stdint.h> */ /* njk */
#include <string.h> /* njk */
#include <assert.h>
typedef unsigned char uint8_t; /* njk */
struct Foo {
uint8_t x;
uint8_t y;
uint8_t z;
uint8_t m[3];
};
struct Bar
{
uint8_t m[3];
};
void pr(const char *title, const void *block, size_t n)
{
uint8_t *p = (uint8_t*)block; /* njk */
size_t i; /* njk */
printf("%s\n", title);
for (i = 0; i < n; ++i) /* njk */
printf("0x%02x ", *p++);
printf("\n");
}
void cp(const struct Foo *foo) /* njk */
{
struct Bar bar; /* njk */
struct Bar *p1 = &bar; /* njk */
struct Bar *p2 = (struct Bar*)(foo->m); /* njk */
pr("before: p1:", p1, 3);
pr("before: p2:", p2, 3);
memcpy(p1, p2, 3);
pr("after: p1:", p1, 3);
pr("after: p2:", p2, 3);
if (memcmp(p1, p2, 3) != 0)
printf("!!! cp is wrong\n");
}
int main()
{
struct Foo foo; /* njk */
foo.x = 1;
foo.y = 2;
foo.z = 3;
foo.m[0] = 0x40;
foo.m[1] = 0x19;
foo.m[2] = 0x21;
cp(&foo);
/* cp2(&foo); */ /* njk */
return 0;
}
***************
the lines I changes are marked /* njk */
This produced the following output
C:\bin\Debug>woody.exe
before: p1:
0x00 0x08 0x00
before: p2:
0x40 0x19 0x21
after: p1:
0x40 0x19 0x21
after: p2:
0x40 0x19 0x21
which looks ok to me
--
Nick Keighley
"Of course I'm going to be in an aeroplane on 31st December 1999.
You scientists wouldn't be stupid enough to build things that don't
work.
Besides what have computers got to do with aeroplanes anyway?"
.
- Follow-Ups:
- Re: Library bug or my fault?
- From: Steven Woody
- Re: Library bug or my fault?
- References:
- Library bug or my fault?
- From: Steven Woody
- Library bug or my fault?
- Prev by Date: Re: Library bug or my fault?
- Next by Date: Re: Library bug or my fault?
- Previous by thread: Re: Library bug or my fault?
- Next by thread: Re: Library bug or my fault?
- Index(es):
Relevant Pages
|