Why size of an empty function is 5?

Questions by Sheena_sankar   answers by Sheena_sankar

Showing Answers 1 - 36 of 36 Answers

Pradeep

  • Mar 5th, 2007
 

How can you calculate size of a function?
There is nothing like size of function.
Size of function may include the number of lines in the code.
Logic complexity of the function.
Size of function can not be calculated in terms of using sizeof function.

googleever

  • Mar 6th, 2007
 

I tried on sun4u sparc SUNW,Sun-Fire-V440 machine and i got size of function as 4 bytes. I used sizeof function itself to determine the size of the empty function.

Can somebody explain why it returned 4 bytes ?

Richard

  • Mar 8th, 2007
 


I believe functions can sometimes use 4 bytes to keep a reference to the vtable (virtual table).

  Was this answer useful?  Yes

Nagendra

  • Mar 13th, 2007
 

Pradeep,
You are absolutely wrong. sizeof() library function will find the size of function also.
Try below code ..
#include<iostream>
typedef struct A
{
        int a;
        float b;
        long c;
}A1;


A1 a()
{
int a;int b;
a=12;b=13;b=a+b;
 }

int main()
{
        cout<<sizeof(a())<<endl;
        return 0;
}

Now your second & third points are not valid, why because here sizeof function always give the return type of function.

Best regards
NAGENDRA

  Was this answer useful?  Yes

Ahn gun-ho

  • Mar 15th, 2007
 

sizeof(f())   is not the sizeof function 'f()'

it's sizeof the return value of the function f()

Gunvant

  • Mar 22nd, 2007
 

#include <stdio.h>


void foo() {
}
int main() {
        printf("Sizeof Foo(): %d", sizeof(foo));
        return 0;
}
            

output :
Sizeof Foo(): 1

with gcc version 4.0.0 20050519

  Was this answer useful?  Yes

ya_bolek

  • Mar 25th, 2007
 

It might be helpful to what exactly happens in line:

cout<<sizeof(a())<<endl;

This line actually makes a call to the function a. Put a cout statement inside the function body and you'll see the proof.


So to re-iterate, it's meaningless to speak of the size of a function. However, it does make sense to speak about the size of an address of a function. Tha size varies depending on the machine architecture and will typically be either 4 or 8 for 32 and 64 bit architectures respectively.

cout<<sizeof(&a);

  Was this answer useful?  Yes

Rahul_ars

  • Jul 17th, 2008
 

just try this program with changing return type...

#include <iostream.h>
#include <conio.h>
typedef double ret;

ret f();

int main(void)
{
cout<<"Size of funcion : "<<sizeof(f());
getch();
return 0;
}

ret f()
{
ret x=0;
return x;
}

  Was this answer useful?  Yes

OSaienni

  • Sep 12th, 2008
 

ya_bolek,


You are partially right.

sizeof( anything ) actually doesn't run any expressions. Here is an example I found on this site.

int i = 10;

sizeof(++i + 10); <- returns 4 on 32 bit.

after execution will still be 10

When we sizeof(func()); It does infact just test the return type but never executes the function.

Also, 100% right about sizeof(&func) returning the size of the function pointer.

ajrobb

  • Sep 22nd, 2010
 

It isn't.

#include <iostream>

void f() {}

int main() {
  std::cout << sizeof f() << 'n'; // fails to compile on g++ because return is void
  std::cout << sizeof f << 'n' // fails to compile because it is illegal
}

NOTE: "sizeof" is an operator and NOT a function. It does not need parentheses unless you want the sizeof a cast operator, e.g. sizeof (int)

NOTE: it is often bad practice to use endl instead of 'n' because it is defined as widen('n') << flush and this seriously slows down the program.

  Was this answer useful?  Yes

There is no function to calculate the size of a function.


And this 5 value of an empty function is a myth.
Certain working programs can even have size of 4.

Regards

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions