Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the size of a function in C always 1 byte?

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

like image 933
user1645461 Avatar asked Sep 04 '12 07:09

user1645461


People also ask

What is the size of C function?

The sizeof operator is the most common operator in C. It is a compile-time unary operator and used to compute the size of its operand. It returns the size of a variable. It can be applied to any data type, float type, pointer type variables.

Is sizeof always in bytes?

sizeof always returns size as the number of bytes.

Why do we use size of function?

It helps in providing the byte and size of the variables and the number it occupies for the allocation of the variable to the memory. Sizeof() function is exclusively used to find out the exact size of a type of the variable used for programming in C.


3 Answers

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 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

like image 113
Kerrek SB Avatar answered Oct 24 '22 10:10

Kerrek SB


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 http://gcc.gnu.org/onlinedocs/gcc/Pointer-Arith.html#Pointer-Arith

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.

like image 31
Michael Burr Avatar answered Oct 24 '22 10:10

Michael Burr


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)." https://groups.google.com/forum/?fromgroups=#!msg/comp.std.c/ycpVKxTZkgw/S2hHdTbv4d8J

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.

like image 13
Jon Hanna Avatar answered Oct 24 '22 12:10

Jon Hanna