CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Jun 2009
    Posts
    15

    [RESOLVED] How to determine function size?

    Is there a way to determine the function size, like you can determine a size of structure.

    This is just an example. So, is it possible to determine, how much memory is used, or will be used to store this function, or in other words, size of it.

    Code:
    int calculate(int a, int b){
    
        printf("Result is: %d\n", a + b);
    
    return a + b;
    }

  2. #2
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    Re: How to determine function size?

    What do you mean by how much memory is used to "store" the function? Do you mean the amount of executable code that will be generated for the function? Or the size of the stack frame for this function while it executes?
    Old Unix programmers never die, they just mv to /dev/null

  3. #3
    Join Date
    Jun 2009
    Posts
    15

    Re: How to determine function size?

    I guess it would be the size of executable code, or maybe both. But not the size on disk, but the size of RAM memory used to load it.

  4. #4
    Join Date
    Dec 2007
    Posts
    69

    Re: How to determine function size?

    I don't think this is possible. I don't even think a funcion is required to be in just one block (but it is on all PCs I know).

    Why do you want to do that anyway?

    BTW, the size on the disk is the same size that on the memory (at least in Windows).

    If you want to get the size of a function anyway, compile the executable and look at it in a debugger.

  5. #5
    Join Date
    Jun 2009
    Posts
    15

    Re: How to determine function size?

    I was having a discussion with a colleague yesterday on this subject. Is it possible to do it, determine the size?
    His answer was like yours, that is not possible, and I was claiming the opposite.

    I was trying to find a solution asking Google but without any results so i opened a topic.

    I am not good with debuggers and assembly code and such...

    I was thinking of finding function's end then subtracting it with the starting pointer, it would result the memory size used right, but i don't have the idea how to find the end of it??

  6. #6
    Join Date
    Aug 2007
    Posts
    858

    Re: How to determine function size?

    I was having a discussion with a colleague yesterday on this subject. Is it possible to do it, determine the size?
    The problem is, what exactly are you going to measure? For a function like

    Code:
    void Foo(size_t i)
    {
      int* ptr = new int[i];
      //...
      delete ptr;
    }
    What's the size? You can't know the true memory usage until runtime. But even if you only count "size" as stack objects, what about

    Code:
    void Foo(int i)
    {
      int j = i - 1;
      cout << j << endl;
      if (j > 0)
        Foo(j);
    }
    No dynamic allocation. But you still don't know how much it's going to drop on the stack until runtime. So what's its size?

  7. #7
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    103

    Re: How to determine function size?

    There was this trick i used back in the VS6 days when i made aim bots for FPS games. I cant garentee it will work but u can try it.

    Code:
    void bla()
    {
    }
    static char bla_end = 0;
    
    int size = (&bla_end) -  ((char*)&bla)
    i am not sure if it was looking like this... I forgot . :P

    or if u can get the adress of goto's (not sure if u can do this)
    or u can try calling 2 setjmp's and calculate from there.



    but the ultimate solution:
    make the function an export. goto assembly output, goto address of funcvtion (by folowing eexported adress) than count how big it is by getting the last return of the function. Than patch your exe, try setting a variable to 0xDEADF00D so it will be easy to find.
    Last edited by wigga; June 12th, 2009 at 05:15 PM.

  8. #8
    Join Date
    Apr 2009
    Posts
    598

    Smile Re: How to determine function size?

    Easy! You can use the old trick of programmers working in assembly who are counting the size of code by measuring the distance between two labels.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int calculate(int a, int b)
    {
       printf("Result is: &#37;d\n", a + b);
    
       return a + b;
    }
    
    int after_calculate(void)
    {
       return 0;
    }
    
    int main(int argc, char *argv[])
    {
      printf("Size of calculate is: %d - %d = %d.",
             (long int)(&after_calculate),
             (long int)(&calculate),
             (long int)(&after_calculate) - (long int)(&calculate));
    
      system("PAUSE");	
      return 0;
    }
    I get 36 bytes, and it looks good to me.

    Edit: Wigga is quicker than me.

  9. #9
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    103

    Re: How to determine function size?

    I was quicker but your code is looking alot better

  10. #10
    Join Date
    Jun 2009
    Posts
    15

    Re: How to determine function size?

    Thank you - both of you

    That was what i was up to, but i didn't know how to find "the end".

    Quote Originally Posted by Not me View Post
    I was thinking of finding function's end then subtracting it with the starting pointer, it would result the memory size used right, but i don't have the idea how to find the end of it??

  11. #11
    Join Date
    May 2009
    Posts
    2,413

    Re: [RESOLVED] How to determine function size?

    Quote Originally Posted by Not me View Post
    Is there a way to determine the function size, like you can determine a size of structure.
    The size of a function is not defined by the C++ language (to the best of my knowledge) so in that sense it's not possible.

  12. #12
    Join Date
    May 2009
    Location
    Netherlands
    Posts
    103

    Re: [RESOLVED] How to determine function size?

    I think however if you make both functions C export functions than there is more garentee that it will be one block and that both addresses wil be in the valid position. Because your not giving thhe compiler the chance to do it in a diffrent way than putting it in one block or other order.. Again i am not sure.

    It all depends on the compiler.

  13. #13
    Join Date
    Nov 2006
    Location
    ntdll.dll
    Posts
    29

    Re: [RESOLVED] How to determine function size?

    Code:
    int GetFunctionSize( DWORD dwStart )
    {
    	int i = 0;
    
    	for( i = 0; *( BYTE* )( i + dwStart ) != 0xC3; i++ )
    		continue; 
    
    	return ++i;
    }
    
    GetFunctionSize( ( DWORD )Calculate );
    I've used this before.
    Tom

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured