From 65e12e036264faa2872b1dba426dbc7b1fad7dd8 Mon Sep 17 00:00:00 2001 From: "nethack.rankin" Date: Tue, 11 Jul 2006 04:08:24 +0000 Subject: [PATCH] union bit (trunk only) 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 | 6 +++--- src/hack.c | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/decl.c b/src/decl.c index 4640d3534..cba20bfc7 100644 --- a/src/decl.c +++ b/src/decl.c @@ -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; diff --git a/src/hack.c b/src/hack.c index a5b52c8d1..00a072c6d 100644 --- a/src/hack.c +++ b/src/hack.c @@ -61,7 +61,7 @@ void zero_anything(any) anything *any; { - (void) memset((genericptr_t)any, 0, sizeof(anything)); + *any = zeroany; } boolean -- 2.40.0