size of a function

C

Chris Dollin

asit said:
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.
 
W

Walter Roberson

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?
 
M

Malcolm McLean

asit dhal said:
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
 
O

Old Wolf

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.

Been trying your program out?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top