How to calculate size of a function in c?

Size of any function is calculated as:

Size of function = Size of all local variable which has declared in function + Size of those global variables which has used in function+ Size of all its parameter+ Size of returned value if it is an address. Example:

What is size of calculate function?

int x=2;
int* calculate
void main(){
    int *p,a=0,b=5;
    p=calculate(a,b);
    printf(“%d”,*p);
}
int * calculate(int a,int b){
    static int z;
    z=a+x+b;
    return &z;
}

Answer:

Size of calculate function= 2 (global variable x)+ 2 (local variable z) +2*2 (parameter a, b)+ 2 ( returning address of z)= 10 bytes

2 comments:

mohini said...

i need some more examples

Anonymous said...

Can u please provide example, how to calculate size if dynamic memory allocated ??