]> granicus.if.org Git - nethack/commitdiff
union bit (trunk only)
authornethack.rankin <nethack.rankin>
Tue, 11 Jul 2006 04:08:24 +0000 (04:08 +0000)
committernethack.rankin <nethack.rankin>
Tue, 11 Jul 2006 04:08:24 +0000 (04:08 +0000)
     I'm pretty sure that some pre-standard compilers don't know how to
apply an initializer to a variable of type union.  Unfortunately, I don't
have access to one to check.  Fortunately, there's no need to explicitly
initialize `zeroany' since the default value is what we want--the first
field will be set to zero or null as appropriate (null in this case).

     Strictly speaking, this isn't adequate; what if long is wider than a
pointer rather than narrower?  Using `= {DUMMY}' didn't handle that case
either; the ordering of the union's fields controls which bits get stored.
As a practical matter, it should make no difference.  As long as the code
reading a union accesses the same field as the code writing that union set
up in it, anything in extraneous bits should be irrelevant--except perhaps
when a debugger tries to format things.  The whole issue has always been
implicitly based on the assumption that null pointers have all bits zero
in the first place; that's typical but not guaranteed.

src/decl.c
src/hack.c

index 4640d3534a57fa761765b41215d0301ff5db746f..cba20bfc73d7244a2167df6773f6a8cf587094e3 100644 (file)
@@ -1,4 +1,4 @@
-/*     SCCS Id: @(#)decl.c     3.5     2005/11/19      */
+/*     SCCS Id: @(#)decl.c     3.5     2006/07/10      */
 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
 /* NetHack may be freely redistributed.  See license for details. */
 
@@ -194,8 +194,8 @@ NEARDATA struct obj *billobjs = (struct obj *)0;
 
 /* used to zero all elements of a struct obj */
 NEARDATA struct obj zeroobj = DUMMY;
-/* used to zero out union any*/
-NEARDATA anything zeroany = DUMMY;
+/* used to zero out union any; initializer deliberately omitted */
+NEARDATA anything zeroany;
 
 /* originally from dog.c */
 NEARDATA char dogname[PL_PSIZ] = DUMMY;
index a5b52c8d1b815726b92c6bf47fe83c8ac58ee44a..00a072c6d6668fa5c84c7c3018f379fd10d45136 100644 (file)
@@ -61,7 +61,7 @@ void
 zero_anything(any)
 anything *any;
 {
-    (void) memset((genericptr_t)any, 0, sizeof(anything));
+    *any = zeroany;
 }
 
 boolean