Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sizeof(function) always returns 1.Why? [duplicate]

size of a function using sizeof() always returns 1

I just tried to find the size of a function.While using sizeof() for finding the size it always returns 1.Even the function is a well defined function it just returns 1. I just wanna know why it returns 1. What actually it considers when saying sizeof(funtion).

consider a simple swap function. I used fun3 just to show the difference of addresses occupied.

void fun1(int a,int b)
{
 int temp;
 temp=a;
 a=i;
 i=b;
 printf("%d\t %d\n",a,b);
}
void fun2()
{

}
void fun3()
{

}
void main()
{
 printf("fun1\t Address : %d \t Size : %d\n",fun1,sizeof(fun1));
 printf("fun2\t Address : %d \t Size : %d\n",fun2,sizeof(fun2));
 printf("fun3\t Address : %d \t Size : %d\n",fun3,sizeof(fun3));
}

the output here is
fun1 Address : 4199248 Size : 1
fun2 Address : 4199301 Size : 1
fun3 Address : 4199307 Size : 1

The number of addresses occupied by fun1 is huge,fun2 occupies only 6.But the Size remains same for both

like image 952
HARISH KARTHIK Avatar asked May 17 '26 13:05

HARISH KARTHIK


1 Answers

I suspect that there's a long chain of issues here that leads to the answer of "this is undefined behavior."

The first thing to note is that you shouldn't use %d to print out pointers in printf. The %d modifier is for printing ints, and ints aren't always convertible to and from pointers in the usual way. For example, on a 64-bit system, it might be the case that an int is 32 bits and that a pointer is 64 bits, meaning that passing in a pointer to printf and then trying to print it with %d might only print half the bits. You should use the %p specifier any time you want to print out a pointer.

That would suggest that some amount of what you're seeing here might be due to the %d only picking up half the bits from your pointer, which is then leaving the other bits from the pointer getting picked up by the second %d modifier, meaning that the 1 that you're seeing here isn't actually the size of the function pointer, but rather garbage data due to a bad format string. That would be Undefined Behavior - bad times!

Now, how would you go about printing the address of the function itself? The bad news is that there's no portable way to do this. Normally, for pointers, you could just use the %p modifier. But function pointers are unusual in C in that they can't be implicitly converted to normal pointers. That is, you can't portably cast a function pointer to, say, a void * or a void * to a function pointer. The literal reason for this is "because the spec says so," but the deeper reason is that on some non-x86, non-ARM systems functions are stored in a physically different region of memory than data and the pointers have very different binary representations than regular pointers.

There are some heavyweight ways to print out function pointers, but as you can see, it's not easy.

Independently, how should you print out the size of the function pointer? Try this, using the %zu modifier, which is for size_ts:

printf("Function pointer has size %zu\n", sizeof(&fun1));
like image 52
templatetypedef Avatar answered May 20 '26 18:05

templatetypedef



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!