9

In the gcc compiler, sizeof(main), sizeof(printf) and sizeof(scanf) all are 1. I want to know how the size of all these are 1. What is the logic behind it?

Daren Thomas
  • 67,947
  • 40
  • 154
  • 200
Parikshita
  • 1,297
  • 5
  • 15
  • 23
  • Duplicate: [what does sizeof (function name) return?](http://stackoverflow.com/questions/2666392/what-does-sizeof-function-name-return) – Paul R Oct 01 '10 at 12:09

1 Answers1

25

Because the C(99) standard requires (§6.5.3.4/1)

The sizeof operator shall not be applied to an expression that has function type or an incomplete type, to the parenthesized name of such a type, or to an expression that designates a bit-field member.

so the return value is meaningless. If you need the sizeof the function pointer, use

sizeof(&main)
sizeof(&printf)
sizeof(&scanf)

gcc returns 1 on types that the sizeof is meaningless (see c-common.c):

4187     if (type_code == FUNCTION_TYPE)
4188       {
4189         if (is_sizeof)
4190           {
4191             if (complain && (pedantic || warn_pointer_arith))
4192               pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
4193                        "invalid application of %<sizeof%> to a function type");
4194             else if (!complain)
4195               return error_mark_node;
4196             value = size_one_node;
4197           }
4198         else
4199           value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
4200       }
4201     else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
4202       {
4203         if (type_code == VOID_TYPE
4204             && complain && (pedantic || warn_pointer_arith))
4205           pedwarn (loc, pedantic ? OPT_pedantic : OPT_Wpointer_arith,
4206                    "invalid application of %qs to a void type", op_name);
4207         else if (!complain)
4208           return error_mark_node;
4209         value = size_one_node;
4210       } 
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • So applying sizeof to a function type is undefined/unspecified behaviour? – codymanix Oct 01 '10 at 12:01
  • thnx KennyTM...But I want to know how the gcc compiler returns 1 ?? – Parikshita Oct 01 '10 at 12:02
  • 11
    @Parixit: Because you did something that makes no sense, so you get a result that makes no sense. Garbage in, garbage out. It's undefined behavior, and the compiler could do anything it likes, including formatting your harddrive or setting your hair on fire. But it chose to do something a bit less dramatic, and just returned 1 – jalf Oct 01 '10 at 12:06
  • I guess that function as is have no size (size=0), but sizeof is required to return something > 0. And yes, a compile error instead would have made more sense to me. – codymanix Oct 01 '10 at 12:10
  • @Parixit The gcc developers decided to hardcode that `sizeof function` is 1. – nos Oct 01 '10 at 12:11
  • 2
    As far as the standard is concerned, shouldn't this result in a diagnostic, rather than undefined behavior, since it's a breach of language constraints? Indeed, with `-pedantic`, gcc issues a warning. Obviously in non-standard modes (without `-pedantic`), gcc can do what it likes: if you're not following the standard, then *everything* is undefined behavior by definition. – Steve Jessop Oct 01 '10 at 12:38
  • 2
    @jaif: it should SO set his hair on fire. –  Oct 01 '10 at 13:38