-
June 12th, 2009, 11:17 AM
#1
[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;
}
-
June 12th, 2009, 12:34 PM
#2
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
-
June 12th, 2009, 03:57 PM
#3
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.
-
June 12th, 2009, 04:06 PM
#4
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.
-
June 12th, 2009, 04:15 PM
#5
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??
-
June 12th, 2009, 04:40 PM
#6
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?
-
June 12th, 2009, 05:13 PM
#7
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.
-
June 12th, 2009, 05:20 PM
#8
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: %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.
-
June 12th, 2009, 05:27 PM
#9
Re: How to determine function size?
I was quicker but your code is looking alot better
-
June 12th, 2009, 05:51 PM
#10
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".
Originally Posted by Not me
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??
-
June 13th, 2009, 12:00 AM
#11
Re: [RESOLVED] How to determine function size?
Originally Posted by Not me
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.
-
June 13th, 2009, 06:23 AM
#12
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.
-
June 13th, 2009, 11:30 PM
#13
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|