serious: GCC 2.95.3 accepts illegal function call
Zack Weinberg
zack@codesourcery.com
Fri Sep 7 11:46:00 GMT 2001
On Fri, Sep 07, 2001 at 08:27:34PM +0200, Michael Noisternig wrote:
> Check this:
>
> --- begin code ---
>
> typedef long long uint64;
>
> uint64 calculate_cpu_timing_factors (unsigned int instr)
> {
> // ...
> }
>
> uint64 calculate_cpu_timing()
> {
> return calculate_cpu_timing(calculate_cpu_timing(10000)/1024);
> }
>
> --- end code ---
>
> This piece of code is happily accepted by the compiler! Check it 'til
> you find the error, it took me the whole day :-(((
Would you believe this is completely valid, if dubious, ANSI C?
The prototype you put on the definition of the second function,
uint64 calculate_cpu_timing() { ... }
does _not_ mean "this function takes no arguments." It means "This
function may be called with any argument list whatsoever." You may be
used to C++, where that prototype _does_ mean "this function takes no
arguments."
If you had written instead
uint64 calculate_cpu_timing(void) { ... }
the compiler would have diagnosed the error you expected it to:
test.c: In function `calculate_cpu_timing':
test.c:10: too many arguments to function `calculate_cpu_timing'
test.c:10: too many arguments to function `calculate_cpu_timing'
I believe this is allowed in C++ too.
[You may wonder why on earth the standard allows such a bizarre thing;
it is for compatibility with pre-standard C, in which there were no
function prototypes.]
zw
More information about the Gcc-bugs
mailing list