Re: size of a function
- From: Joe Wright <jwright@xxxxxxxxxxx>
- Date: Wed, 07 Sep 2005 06:48:45 -0400
Keith Thompson wrote:
You're quite right. I don't know how I got it turned around but I thought it was being argued that 'sizeof main' would give something useful. Sorry for adding only noise.Joe Wright <jwright@xxxxxxxxxxx> writes:
Keith Thompson wrote:
Joe Wright <jwright@xxxxxxxxxxx> writes:
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.
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
.- References:
- size of a function
- From: maadhuu
- Re: size of a function
- From: Walter Roberson
- Re: size of a function
- From: Robert Gamble
- Re: size of a function
- From: Joe Wright
- Re: size of a function
- From: Keith Thompson
- Re: size of a function
- From: Joe Wright
- Re: size of a function
- From: Keith Thompson
- size of a function
- Prev by Date: Re: help me with the program
- Next by Date: Re: implementation of the 'sizeof' operator
- Previous by thread: Re: size of a function
- Next by thread: Re: size of a function
- Index(es):
Relevant Pages
|