Message from discussion
Expert-Q: (a!=b) != memcmp(&a,&b,sizeof a) ?
From: Helmut Leitner <leit...@hls.via.at>
Subject: Expert-Q: (a!=b) != memcmp(&a,&b,sizeof a) ?
Date: 1999/06/28
Message-ID: <377738B4.97C7C3D@hls.via.at>#1/1
X-Deja-AN: 494698941
Content-Transfer-Encoding: 7bit
Organization: HLS
X-Accept-Language: en
Content-Type: text/plain; charset=us-ascii
MIME-Version: 1.0
Newsgroups: comp.lang.c
Let's assume that a and b have the same basic data type
(e.g. int, long, float, double ...).
On many platforms it is possible to replace the
comparisions
a==b
a!=b
by
memcmp(&a,&b,sizeof(a))==0
memcmp(&a,&b,sizeof(a))
What are the conditions that this will not yield the
desired results?
Can someone give (an) implementation example(s), where
the reasons for this behaviour can be seen?
The background is the wish of fast and easy comparison
of multicomponent structures (within a perfect hash system):
typedef struct test {
char ...
int ...
float ...
...
} TEST;
TEST sa,sb;
memset(&sa,'\0',sizeof(TEST)); /* or TestClear(&sa); */
memset(&sb,'\0',sizeof(TEST)); /* or TestClear(&sb); */
...
any standard component assignment. /* or TestSetComponent(&sa,comp) */
...
memcmp(&sa,&sb,sizeof(TEST));
Is it possible to construct such a general system to work
in a portable way on all platforms?
Would it be possible to construct an #if-expression
#if (expression_memcmp_will_do_the_job)
TestEqu(psa,psb) (memcmp(psa,psb,sizeof(TEST))==0)
#else
(psa->comp1==psb->comp2 && psa->comp2==psb->comp2 && .... )
#endif
to react to the different situations?
--
Helmut Leitner leit...@hls.via.at
Graz, Austria www.hls-software.com