-3

How to print size of any function? This question was asked but answer was not particle. According to previous answer it is different, when I used built in function to find the size of function?

 #include<stdio.h>
 void print(void)
 {

    printf("i m in print \n");

 }
 int main()
 {
      printf("%d\n",(int)main-(int)print); // o/p is 20 byte  what is this 20    
                                           //is size of function?previous asked answer
      printf("size=%d\n",sizeof(print));// o/p is 1 byte ? how to get exact plz help?
 }

~

Sumit Kumar
  • 25
  • 1
  • 7
  • http://stackoverflow.com/a/4579589/2410359 is the closest you will get: an answer that somewhat works some of the time - which is effectively your first `printf()`. A robust solution is not possible in C. – chux - Reinstate Monica Aug 19 '14 at 12:53
  • @mike W why u guys marked as duplicate i already mention it not particle i have gone throw it – Sumit Kumar Aug 20 '14 at 04:17
  • Your post "How to print size of any function?" has sub-questions "when I used built in function (`sizeof()`) to find the size of function?", "what is this 20?", "o/p is 1 byte ?",... The duplicate only answers some of those questions. I am confident other SO posts answer the remaining questions. IAC, @Yu Hao answer explains well why `sizeof(print)` --> 1. It is UB, any result is possible. Some systems simple always return 1 on `sizeof(function)` – chux - Reinstate Monica Aug 20 '14 at 05:57

4 Answers4

6

sizeof on a function type violates the constraints of sizoef operator:

C11 §6.5.3.4 The sizeof and _Alignof operators

The sizeof operator shall not be applied to an expression that has function type or an incomplete type, to the parenthesized name of such a type, or to an expression that designates a bit-field member.

Community
  • 1
  • 1
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • And so the code is non-portable. The code appears to assume that functions have some kind of location in memory, but they needn't. In a contrived example, a function pointer could be an index into a table and subtracting function pointers would have absolutely no resemblance to a notion such as "the number of bytes making up the machine-executable instructions for the translated function". In a less contrived example, inline functions might be "found" in many places in the machine-executable instructions, instead of in one place. – Shao Aug 19 '14 at 04:33
  • @Shao: Even if a function is inlined in many places, if its address is ever taken, it has exactly one formal address. – R.. GitHub STOP HELPING ICE Aug 19 '14 at 04:39
  • R..: Agreed. And which, of course we both know, would not occur for the **sizeof** case detailed by this answer, but does occur in the original post's code. – Shao Aug 19 '14 at 04:44
  • @Yu Hao can u please explain what printf("%d\n",(int)main-(int)print); // what is this 20? and why sizeof is throwing 1 ? – Sumit Kumar Aug 19 '14 at 04:47
  • 2
    @SumitKumar It's illegal code, nothing to explain. – Yu Hao Aug 19 '14 at 04:54
5

Function do not generally exist as compact contiguous regions in memory. Which is why it is not possible to find size of a function. Functions do not have size.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • can u please explain what printf("%d\n",(int)main-(int)print); // what is this 20? and why sizeof is throwing 1 ? – Sumit Kumar Aug 19 '14 at 04:35
  • please reply i am not able to figure out whats going in my code? – Sumit Kumar Aug 19 '14 at 04:49
  • 3
    @Sumit Kumar: Your code prints the difference between two integer values obtained by implementation-defined conversion from function pointer type to `int` type. What does it mean? I have no idea. Probably the address difference between entry points of these two functions (which is generally not the "size" of any function). What your `sizeof` does I don't know either. Formally, it does not even compile. It is illegal to apply `sizeof` to functions. In other words, nothing meaningful is going on in your code. Trying to "figure it out" is a waste of time. – AnT stands with Russia Aug 19 '14 at 05:45
0

There is no guaranteed way of determining the size of a function at run-time (and little reason to do so).

Functions are part of text segment (which may or may not be 'heap') or its equivalent for the architecture you use. There's no data past compilation regarding their size, at most you can get their entry point from symbol table (which doesn't have to be available). So you can't calculate their size in practice on most C environments you'll encounter.

Jayesh Bhoi
  • 24,694
  • 15
  • 58
  • 73
0

Generate an object file and then use objdump -S foo.o command to find the function size.

mahendiran.b
  • 1,315
  • 7
  • 13