Joe Wright said:
Keith said:
Robert Gamble wrote:
[...]
Just to clarify: you *can* use sizeof on a function pointer, just not
on the function itself.
Robert Gamble
Really? Given 'int foo(int);' somewhere the compiler will see 'foo' as
the address of a function. There is no information anywhere as to how
many bytes of memory are required to house foo().
Yes, really, you can apply sizeof to a function pointer. The result
is the size of the pointer, not the size of the function.
Note that "sizeof foo" won't do this, since the argument to sizeof is
one of the contexts in which a function name is *not* implicitly
converted to a pointer.
For example:
#include <stdio.h>
int foo(int i)
{
}
int main(void)
{
int (*foo_ptr)(int) = foo;
printf("sizeof foo_ptr = %d\n", (int)sizeof foo_ptr);
return 0;
}
On one implementation I tried, this prints
sizeof foo_ptr = 4
On another, it prints
sizeof foo_ptr = 8
#include <stdio.h>
int foo(void) {
return 0;
}
int main(void)
{
printf("sizeof foo is %d bytes.\n", (int)sizeof foo);
printf("sizeof printf is %d bytes.\n", (int)sizeof printf);
return 0;
}
At my house, gcc 3.1 prints 1 in both cases.
Yes, I get the same result. With "-pedantic", I also get:
tmp.c:9: warning: invalid application of `sizeof' to a function type
tmp.c:10: warning: invalid application of `sizeof' to a function type
We agree that the sizeof a pointer is whatever the size of a pointer
is. My point is that the size of a function is not available. The
compiler cannot know it.
Right, none of this is in dispute.
Looking at the quoted text above, Robert Gamble wrote:
Just to clarify: you *can* use sizeof on a function pointer, just not
on the function itself.
You replied:
Really?
[...]
And I replied:
Yes, really, you can apply sizeof to a function pointer.
[...]
Perhaps you thought that Robert Gamble was saying that applying sizeof
to a function pointer would give you the size of the function. I
don't believe that's what he meant.
If that wasn't the source of the confusion, what exactly did you mean
by your "Really?" above? You seemed to be questioning Robert's
statements, which were perfectly correct.
To summarize:
You cannot determine the size of a function in (unextended) C.
You can determine the size of a function pointer; this is not relevant
to the size of the function itself.
Applying the sizeof operator to a function name is a constraint
violation. The function name is not implicitly converted to a pointer
as it is in most other contexts.
(gcc, as an extension, yields a meaningless value of 1 when the sizeof
operator is applied to a function name. This is a side effect of its
(arguably ill-advised) support of pointer arithmetic on function
pointers. This is entireliy non-standard, though gcc is permitted to
do this as long as it issues a diagnostic in conforming mode.)
I don't recall seeing any statements in this thread that contradict
any of this.