* comparison of structures
@ 2006-05-18 14:10 José Bollo
2006-05-18 14:19 ` Fabian Cenedese
2006-05-18 14:29 ` cheng long
0 siblings, 2 replies; 3+ messages in thread
From: José Bollo @ 2006-05-18 14:10 UTC (permalink / raw)
To: gcc-help
hi,
the following C code
typedef struct {
int32_t a;
void *b;
} S;
void fs() {
char t[255];
int i;
for(i=0;i<255;i++)
t[i]=(char)(i+1);
}
int cmp1(S a1, S a2) {
return !memcmp(&a1,&a2,sizeof(S));
}
int cmp2(S a1, S a2) {
return a1.a==a2.a && a1.b==a2.b;
}
void put(S y) {
int a,b;
S x;
x=y;
a=cmp1(x,y);
b=cmp2(x,y);
printf("a=%d b=%d\n",a,b);
}
int main() {
S a;
a.a=1;
a.b=&a;
fs();
put(a);
return 0;
}
compiled for my linux x86_64 using the amd64 ABI (-march=athlon64) with gcc
3.4.4 prints "a=0 b=1"
i have seen why and it is clear to me how a such result is produced.
i think that it is not a bug and that the structure can not be compared using
'memcmp'.
do you agree or it a bug ?
regards
josé
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: comparison of structures
2006-05-18 14:10 comparison of structures José Bollo
@ 2006-05-18 14:19 ` Fabian Cenedese
2006-05-18 14:29 ` cheng long
1 sibling, 0 replies; 3+ messages in thread
From: Fabian Cenedese @ 2006-05-18 14:19 UTC (permalink / raw)
To: gcc-help
>the following C code
>
>typedef struct {
> int32_t a;
> void *b;
>} S;
>
>compiled for my linux x86_64 using the amd64 ABI (-march=athlon64) with gcc
>3.4.4 prints "a=0 b=1"
>
>i have seen why and it is clear to me how a such result is produced.
>i think that it is not a bug and that the structure can not be compared using
>'memcmp'.
>
>do you agree or it a bug ?
You can use memcmp if you fill up the struct with a dummy value that you
can set explicitely. Or use a memset(&a, 0, sizeof(S)) before doing any
work. Both are workarounds but I wouldn't say that it's a bug.
bye Fabi
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: comparison of structures
2006-05-18 14:10 comparison of structures José Bollo
2006-05-18 14:19 ` Fabian Cenedese
@ 2006-05-18 14:29 ` cheng long
1 sibling, 0 replies; 3+ messages in thread
From: cheng long @ 2006-05-18 14:29 UTC (permalink / raw)
To: José Bollo; +Cc: gcc-help
In computer, the result is a=1, b=1.
My box isFC5 on IA-32.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2006-05-18 14:29 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-05-18 14:10 comparison of structures José Bollo
2006-05-18 14:19 ` Fabian Cenedese
2006-05-18 14:29 ` cheng long
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).