Re: size of a function



Keith Thompson wrote:
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.

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
"Everything should be made as simple as possible, but not simpler."
                    --- Albert Einstein ---
.



Relevant Pages

  • Re: problem with memcpy and pointers/arrays confusion - again
    ... int line, unsigned long *total_mem) ... That's a long pointer address... ... If sizeof > sizeof which is ... if you allocate for char with sizeof < sizeof, ...
    (comp.lang.c)
  • Re: Virtual function and multiple inheritance
    ... virtual int func(); ... class Derived: Foo, Goo { ... Here pF is a pointer to an object with the same memory layout as a Foo and pG is a pointer to an object with the same memory layout as a Goo. ...
    (microsoft.public.vc.language)
  • Re: About C++ memory management
    ... int * ptr = new int; ... delete ptr; ... But if I copy this pointer like below should I also delete copied ... Foo ...
    (microsoft.public.vc.language)
  • Re: would C be easier to read if...
    ... That indicates that it's a pointer to a function. ... int *a a is pointer to int ... In my case a type declaration that reads linearly from left to right would ... Let's say we have a regular int identifier named "foo". ...
    (comp.lang.c)
  • Re: help with left-right cdecl rule
    ... This "const int * foo" also makes sense, foo is a pointer to a const ...
    (comp.lang.c)