GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 121 of 203    Print  
Why size of an empty function is 5?

  
Total Answers and Comments: 10 Last Update: September 12, 2008     Asked by: Sheena_sankar 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: Pradeep
 
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.


Above answer was rated as good by the following members:
yzesong
March 05, 2007 10:16:40   #1  
Pradeep        

RE: Why size of an empty function is 5?
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.

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
March 06, 2007 00:29:38   #2  
googleever        

RE: Why size of an empty function is 5?
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 ?

 
Is this answer useful? Yes | No
March 08, 2007 16:51:31   #3  
Richard        

RE: Why size of an empty function is 5?

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

 
Is this answer useful? Yes | No
March 13, 2007 06:59:46   #4  
Nagendra        

RE: Why size of an empty function is 5?

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


 
Is this answer useful? Yes | No
March 15, 2007 21:43:32   #5  
Ahn gun-ho        

RE: Why size of an empty function is 5?
sizeof(f()) is not the sizeof function 'f()'

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

 
Is this answer useful? Yes | No
March 22, 2007 08:15:30   #6  
Gunvant        

RE: Why size of an empty function is 5?
#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

 
Is this answer useful? Yes | No
March 25, 2007 14:03:37   #7  
ya_bolek        

RE: Why size of an empty function is 5?
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);

 
Is this answer useful? Yes | No
June 18, 2008 09:10:30   #8  
chiyuwang Member Since: June 2008   Contribution: 4    

RE: Why size of an empty function is 5?
1 byte for return value
1 byte for function name
1 byte for parameters
1 byte for empty body

 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
July 17, 2008 11:03:39   #9  
Rahul_ars Member Since: July 2008   Contribution: 1    

RE: Why size of an empty function is 5?
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;
}

 
Is this answer useful? Yes | No
September 12, 2008 06:57:52   #10  
OSaienni Member Since: September 2008   Contribution: 7    

RE: Why size of an empty function is 5?
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.

 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape