(j3.2006) Interesting F2003-ism

Malcolm Cohen malcolm
Fri Feb 23 05:49:23 EST 2007


Andy Vaught said of Michael's though experiment:
>   But you can't implement a C ABI that way.

Possibly not, but there is actually quite a lot of leeway.

>  Variable argument lists have
> to be passed in the same manner as fixed argument lists.

You say that, but I am less than unconvinced.

>  Otherwise, a
> variable argument list function won't work when invoked through a function
> pointer that has been cast around a bit.

And many of those cases are illegal according to the C standard.

How far the few legal cases constrain how the ABI is forced to be
implemented is not entirely clear.  I've certainly heard of implementations
that work similarly (not quite the same) to what Mike was suggesting.
Of course there need not even be an ABI...

What *is* entirely clear is that you have to call a function that is
defined with an ellipsis with a prototype, and that prototype must
use ellipsis the same as the function definition.

This follows reasonably straightforwardly from p120 of c99.

So your printf examples are just wrong.

Whether gcc "lets them work" is a rather uninteresting question.
We didn't add BIND(GCC), we added BIND(C).

In fact my copy of gcc 3.4.4 doesn't allow it anyway:
% cat junkz.c
s1() { extern printf(char *,int); printf("%d",3); }
s2() { extern printf(char *,float); printf("%e",3.5f); }
% gcc junkz.c
junkz.c: In function `s1':
junkz.c:1: warning: conflicting types for built-in function 'printf'
junkz.c: In function `s2':
junkz.c:2: error: conflicting types for 'printf'
junkz.c:1: error: previous declaration of 'printf' was here

Cheers,
-- 
........................Malcolm Cohen (malcolm at nag-j.co.jp), Nihon NAG, Tokyo.



More information about the J3 mailing list