Thread: sizeof function

  1. #1
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501

    sizeof function

    Is there a way to find the sizeof a function, I haven't tried, but I would think sizeof (printf("hi")); would return the size of what printf returned, an int.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It isn't a function. It's an operator.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    I know sizeof is an operator, I want to know if there is a way to find how much space a function is taking up in memory, if any at all.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Is there a way to find the sizeof a function
    No. The first thing the C standard says about sizeof in section 6.5.3.4 is that it "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 expresison that designates a bit-field member."
    My best code is written with the delete key.

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    void Func1(void) 
    {
    	...
    }
    
    void Func2(void) 
    {
    	...
    }
    
    size_t cbFunc1 = (char *) Func2 - (char *) Func1;
    If the linker has been kind enough to lay the functions out in order. What do you need it for?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by anonytmouse
    Code:
    size_t cbFunc1 = (char *) Func2 - (char *) Func1;
    If the linker has been kind enough to lay the functions out in order. What do you need it for?
    As mentioned in the thread on pointer math, this is undefined behaviour, since you're not addressing members of an array (or the space one beyond said array).

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You can't calculate the 'size of' a function in C. First of all, there is no built-in facility to do it. And since C is a hi-level language, each compiler will generate different code because of it's own idiosyncracies, not to mention the specific requirements of each individual platform and CPU. Using assembly you could do it quite easily, of course.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    As mentioned in the thread on pointer math, this is undefined behaviour, since you're not addressing members of an array (or the space one beyond said array).
    So can we legally avoid the pointer maths?

    Code:
    size_t cbFunc1 = (size_t) Func2 - (size_t) Func1;
    Anyway, "undefined" doesn't mean "never works", both examples work on MSVC. Of course, changing compilers or even changing linker options is likely to break this method. However, once the code is compiled, this method should work (or not work) reliably.

    Using assembly you could do it quite easily, of course.
    Could I ask how?

    P.S
    Of course, changing compilers or even changing linker options is likely to break this method.
    LCC-WIN32 chokes on both of these examples with an internal error!

  9. #9
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Thanks.

    I'm not too knowledgable on assembly. Does that mean the function must be assembly also or is there a way to figure out the size of a C function reliably with assembly?

    EDIT: What the!!!!!

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Could I ask how?


    Code:
    .data
    
    fmt db "Size of 'myfunc': %d", 0x0d, 0x0a, 0x00
    size dd 0x00000000
    
    .code
    
    start:
    
    jmp > calc
    
    myfunc:
    
    xor eax, eax
    mov eax, 0  
    ret
    
    boundary:
    
    calc:
    
    mov eax, boundary - myfunc; <--- subtract labels
    mov [size], eax  
    push [size]
    push offset fmt
    call printf
    add esp, 8
    xor eax, eax
    ret
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Sorry, I was trying to make the code more readable.


    >> Does that mean the function must be assembly also or is there a way to figure out the size of a C function reliably with assembly?

    That's really two separate questions.

    >> Does that mean the function must be assembly...

    A procedure is a procedure. It begins at some offset and ends with some sort of ret.

    >> is there a way to figure out the size of a C function reliably with assembly

    Sure. Dissasemble the code and do the measuring manually.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  3. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM