Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 21 Feb 2001 08:53:57 -0700
From:      Warner Losh <imp@village.org>
To:        "Jacques A. Vidrine" <n@nectar.com>
Cc:        freebsd-hackers@FreeBSD.ORG
Subject:   Re: portability sanity check 
Message-ID:  <200102211553.f1LFrvs07412@billy-club.village.org>
In-Reply-To: Your message of "Wed, 21 Feb 2001 09:42:29 CST." <20010221094228.A93221@hamlet.nectar.com> 
References:  <20010221094228.A93221@hamlet.nectar.com>  

next in thread | previous in thread | raw e-mail | index | archive | help
In message <20010221094228.A93221@hamlet.nectar.com> "Jacques A. Vidrine" writes:
: Likewise if the first member were a more complex data type, but
: nevertheless the same between the different structures.
: 
: It seems safe to me, but I can't explain why :-)

It is obfuscated 'C', but it is safe.  The standard requires that
(void *) &foo == (void *) &foo->s and that if s were a complex
structure that it be laid out the same in all instances of s.  So I
think that it is "safe" to do that.

There are times that you'd want to do this (like generic list
routines), but such type punning, as this is known, is error prone and
can lead to problems.  It is best avoided in favor of macros, unions
or some other less error prone technique.

Warner

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200102211553.f1LFrvs07412>