By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,794 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

size of a function

P: n/a
Can any one write a code to measure the size of a function in gcc or vc
++ ?

Feb 19 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.

Feb 19 '07 #2

P: n/a
In article <11**********************@a75g2000cwd.googlegroups .com>,
asit dhal <pe********@gmail.comwrote:
>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
Feb 19 '07 #3

P: n/a

"asit dhal" <pe********@gmail.comwrote 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
Feb 19 '07 #4

P: n/a
On Feb 20, 9:53 am, "Malcolm McLean" <regniz...@btinternet.comwrote:
>
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?

Feb 20 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.