C
Chris Torek
Even assuming both types are passed in the *same* register, the code
can fail if a null pointer isn't represented as all-bits-zero. There
may be other ways it can fail that I can't think of off the top of my
head.
The usual one is that you run out of argument registers, so that
remaining arguments are passed by some other mechanism, where the
size does matter. For instance, on MIPS, you get four "integer-like"
arguments via registers, the rest via temporary storage the compiler
allocates in an appropriate stack frame; on SPARC you get six; on
PowerPC the number depends on whose ABI you use, I believe.
Thus:
printf("%p", 0);
"just happens" to work, but:
printf("i: %d, s: %s, c: %c, p: %p", i, s, c, 0);
"just happens" to behave bizarrely on a 64-bit MIPS. (The four
arguments a0 through a3 include the format and i, s, and c; the
fifth argument for %p -- given incorrectly here as an unadorned
integer constant zero -- writes only 32 of the 64 bits that will
be read.)
The same code "just happens" to work on the SPARC until two more
parameters are inserted.
Yup. In fact, it invokes undefined behavior, with all that implies.
Indeed.