Thread: Size of the function

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    347

    Size of the function

    I wanted to know the size of function. I have written the following code
    Code:
    #include <stdio.h>
    void (*p)(int); 
    void test_func(int);
    int main(void)
    {
        p = test_func;
        (*p)(4);
        printf("%d",sizeof(test_func));
        return 0;
    }
    void test_func(int data)
    {
      printf("%d\n",data);
    }
    Now I used the sizeof of function, as of now it gives me 1. why? I want to know the size of the entire function. How to write a program for the same. Please help.
    Last edited by Satya; 07-27-2015 at 11:18 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why do you want to know this?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    It is required that after every new feature implementation I should go through the linker file and report the size of the complete code. So, I wanted to know instead of going through the linker file I generate it on my own. Yes it can done by seeing the linker file output and I think it is highly compiler specific thing.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Satya
    Now I used the sizeof of function, as of now it gives me 1. why?
    It is against the language rules, so the behaviour is undefined:
    Quote Originally Posted by C11 Clause 6.5.3.4 Paragraph 1a
    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.
    You could have written sizeof(p), but that would give you the size of the function pointer, not the function.

    Quote Originally Posted by Satya
    It is required that after every new feature implementation I should go through the linker file and report the size of the complete code. So, I wanted to know instead of going through the linker file I generate it on my own. Yes it can done by seeing the linker file output and I think it is highly compiler specific thing.
    I do not think that there is any standard way of doing this in C, so you might as well write a script to parse the linker file output or otherwise obtain your desired value.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2015
    Location
    Delhi
    Posts
    35
    why we can't know the size of a function? Read about stack frame and what happens when a function is called -> then it will be clear (if not then further discussion can lead to conclusion). Understanding the Stack
    Last edited by smrtshrm; 07-28-2015 at 12:30 PM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by smrtshrm
    If you want to know about size of a function from the place where it has been called (main() or any other function) through sizeof, then you will come to know only about the size of return value of that function, if it returns nothing then size is 1 else it depends on the type of thing that function returns, like if it returns integer then sizeof will give
    you 4 as result.
    Err... no. Refer to my previous post.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jun 2015
    Location
    Delhi
    Posts
    35
    thanks for pointing it, I took the wrong channel.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. size of array - why function gives size ONE only
    By noob123 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2009, 05:20 PM
  2. Size of array in function
    By slippy in forum C Programming
    Replies: 2
    Last Post: 10-04-2009, 05:28 PM
  3. Size of a function pointer?
    By someprogr in forum C Programming
    Replies: 4
    Last Post: 10-31-2008, 08:42 AM
  4. function/operator size??
    By studentc in forum C Programming
    Replies: 7
    Last Post: 05-24-2004, 02:02 PM
  5. Size of function arguments?
    By Drekenzin in forum C++ Programming
    Replies: 3
    Last Post: 02-17-2002, 06:04 PM