size of a function

Discussion in 'C Programming' started by asit dhal, Feb 19, 2007.

  1. asit dhal

    asit dhal Guest

    Can any one write a code to measure the size of a function in gcc or vc
    ++ ?
    asit dhal, Feb 19, 2007
    #1
    1. Advertising

  2. asit dhal

    Chris Dollin Guest

    asit dhal wrote:

    > Can any one write a code to measure the size of a function in gcc or vc
    > ++ ?


    What would you do with the answer [1], and why?

    [1] To the question "what's the size of this function", not "Can anyone
    write ...", to which the answer is clearly "yes", since
    compiler-writers exist.

    --
    Chris "electric hedgehog" Dollin
    Meaning precedes definition.
    Chris Dollin, Feb 19, 2007
    #2
    1. Advertising

  3. In article <>,
    asit dhal <> wrote:
    >Can any one write a code to measure the size of a function in gcc or vc
    >++ ?


    Maybe -- but not in standard C.

    You haven't really defined what is included in "the size" of
    a function. If the function has uninitialized static variables,
    then do you include the size of the automatic-initialized-to-zero
    region used? If the function has initialized static variables,
    then do you include the size of the initialization block (which
    might be in a completely different code segment and might happen
    to merge initializations of multiple variables from different
    functions)? On architectures that are not stack-oriented, do you
    include the size of the caller save area? Does "the size" of
    the function include any temporary non-heap (e.g., stack) required
    for it to execute?

    If an optimizer detects common code between the function and
    a different function, it could merge the two paths together
    as long as it had some method of determining when to split apart
    again (and if the code was the tail end of the code, that might
    just be the "return from subroutine" code.) When code overlaps like
    that, which of the functions do you count the size against?

    --
    All is vanity. -- Ecclesiastes
    Walter Roberson, Feb 19, 2007
    #3
  4. "asit dhal" <> wrote in message
    > Can any one write a code to measure the size of a function in gcc or vc
    > ++ ?
    >


    void fsize( void (*fptr)() )
    {
    unsigned char bigbuff[1024*32];
    void (*tptr)() = ( void (*) ()) bigbuff;
    int len;

    for(len = 1024*32; len >= 0; len--)
    {
    printf("trying %d\n", len);
    memcpy(bigbuff, fptr, len);
    (*tptr)();
    }
    }

    There's a sporting chance that the this will print out values until it
    segfaults. The last value is the size of your function.

    When the
    Malcolm McLean, Feb 19, 2007
    #4
  5. asit dhal

    Old Wolf Guest

    On Feb 20, 9:53 am, "Malcolm McLean" <> wrote:
    >
    > void fsize( void (*fptr)() )
    > {
    > unsigned char bigbuff[1024*32];
    > void (*tptr)() = ( void (*) ()) bigbuff;
    > int len;
    >
    > for(len = 1024*32; len >= 0; len--)
    > {
    > printf("trying %d\n", len);
    > memcpy(bigbuff, fptr, len);
    > (*tptr)();
    > }
    >
    > }
    >
    > There's a sporting chance that the this will print out values
    > until it segfaults. The last value is the size of your function.


    Not really, the second and subsequent memcpy's will have no effect.
    You would do better (?) to memset the rest of the buffer to 0 or
    some trap value in each case.

    >
    > When the


    Been trying your program out?
    Old Wolf, Feb 20, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Madhanmohan S
    Replies:
    5
    Views:
    7,710
    Madhanmohan S
    Sep 3, 2004
  2. Andreas Klemt
    Replies:
    6
    Views:
    11,003
    Peter Theill
    Nov 28, 2004
  3. =?Utf-8?B?QmlkYXJrb3Rh?=

    How to set the DataGrid Row Size to a fixed size.

    =?Utf-8?B?QmlkYXJrb3Rh?=, May 19, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    813
    =?Utf-8?B?QmlkYXJrb3Rh?=
    May 19, 2005
  4. Neil Zanella
    Replies:
    4
    Views:
    474
    Gianni Mariani
    Oct 9, 2003
  5. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,535
    Michael Jung
    May 25, 2008
Loading...

Share This Page