Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 535 Vote(s) - 3.39 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why is the size of a function in C always 1 byte?

#1
When we check the size of a function using `sizeof()`, we always get **1 byte**.
What does this 1 byte signify?
Reply

#2
It signifies that the compiler writer decided on a value of 1 rather than make demons fly from your nose (indeed, it was another undefined use of `sizeof` that gave us that expression: "the C compiler itself MUST issue a diagnostic IF this is the first required diagnostic resulting from your program, and then MAY itself cause demons to fly from your nose (which, by the way, could well BE the documented diagnostic message) just as it MAY issue further diagnostics for further violations of syntax rules or constraints (or, for that matter, for any reason it chooses)."

[To see links please register here]


From this there's slang term "nasal demons" for whatever a compiler decides to do in response to an undefined construct. `1` is the nasal demon of this compiler for this case.
Reply

#3
As others pointed out, sizeof() can take any valid identifier, but it won't return a valid (an honestly true and valid) result for function names. Furthermore, it definitely may, or may not, result in "demons out of nose" syndrome.

If you want to profile your program function size, check the linker map, which can be found on the intermediate results directory (the one where things are compiled into .obj/.o or where the resulting image/executable lays). Sometimes there's an option to generate or not this map file... it's compiler/linker dependent.

If you wanted the size of a pointer to a function, they're all the same size, the size of an addressing word on your cpu.
Reply

#4
This is not undefined behavior - the C language standard requires a diagnostic when using the `sizeof` operator with a function designator (a function name) since it's a constraint violation for the `sizeof` operator.

However, as an extension to the C language, GCC allows arithmetic on `void` pointers and function pointers, which is done by treating the size of a `void` or a function as `1`. As a consequence, the `sizeof` operator will evaluate to `1` for `void` or a function with GCC. See

[To see links please register here]


You can get GCC to issue a warning when using `sizeof` with these operands by using the `-pedantic` or `-Wpointer-arith` options to GCC. Or make it an error with `-Werror=pointer-arith`.
Reply

#5
It's a constraint violation, and your compiler *should* diagnose it. If it compiles it in spite of that, your program has undefined behaviour [thanks to @Steve Jessop for the clarification of the failure mode, and see [@Michael Burr's answer](

[To see links please register here]

) for why some compilers allow this]: From C11, 6.5.3.4./1:

> The `sizeof` operator shall not be applied to an expression that has function type
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through