makenode() gotcha

VLD/VMB gwyn at BRL.ARPA
Fri Nov 15 18:18:09 AEST 1985


I suppose it's time to post the answer to the puzzle.

The posted code suffered from an alignment problem.  The
actual function parameter `value' (a double) had to be
doubleword-aligned (restriction due to target machine
architecture), but no such restriction applied to other
parameter types, which were all word-aligned.  The attempt
to coerce the address of the formal (struct node *) parameter
to interpret it as the address of a double therefore was
foiled by the fact that there was some padding on the stack
before the actual double datum.  Note that a similar problem
could occur on a big-endian machine without any alignment
restrictions, if addresses point at the "wrong end" of
multi-byte data.

There are several possible fixes to the posted code, but
Howard Johnson suggested what I also believe to be the
best solution, which is to stop trying to multiplex the
uses of the makenode() function and instead create
separate functions for different node types.

Notice that there is a natural tendency when fixing bugs
in existing code to tweak what is already being done,
rather than replace it with a better approach.



More information about the Comp.lang.c mailing list