M
maadhuu
does it make sense to find the size of a function ???
something like sizeof(main) ???
thanking you
ranjan.
something like sizeof(main) ???
thanking you
ranjan.
maadhuu said:does it make sense to find the size of a function ???
something like sizeof(main) ???
thanking you
ranjan.
does it make sense to find the size of a function ???
something like sizeof(main) ???
does it make sense to find the size of a function ???
Nope.
something like sizeof(main) ???
Walter said:No.
gcc without --pedantic allows it, but the size it returns is 1
(at least for my test cases). With --pedantic, or if I
use a stricter compiler, it complains at compile time.
Remember, there isn't anything you can -do- to a function
pointer, except to store it or call through it (or convert it
to void*).
i don't think there is anything wrong in knowing such things , not
necessary that it should be always used somewhere,
also why is it a violation ??
maadhuu said:does it make sense to find the size of a function ???
something like sizeof(main) ???
No.
gcc without --pedantic allows it, but the size it returns is 1
(at least for my test cases). With --pedantic, or if I
use a stricter compiler, it complains at compile time.
Remember, there isn't anything you can -do- to a function
pointer, except to store it or call through it (or convert it
to void*). sizeof a function might make sense if you could
index the function object in some way, but you cannot
(not without invoking undefined behaviour.)
Alexei A. Frounze said:void myfunc(){/*body of the func whose size you want*/}
void bar(){}
bar-myfunc may tell you the size.
But it *may not* just as well due to optimizations and code reordering. I'd
not rely on this, there's no any guarantee here.
Robert Gamble said:Please provide context when posting a followup so we know what you are
responding to.
Agreed, if you don't ask you might never know.
Because the Standard says so.
Robert said:Please provide context when posting a followup so we know what you are
responding to.
Agreed, if you don't ask you might never know.
Because the Standard says so.
Keith Thompson said:No, the "-" operator is not defined for function pointers.
*If* an implementation allows function pointers to be converted to
char*, then
(char*)bar - (char*)myfunc
might give you something meaningful. But conversion of a function
pointer to char* isn't legal either, though some implementations may
allow it.
Eric Sosman said:Robert said:Please provide context when posting a followup so we know what you are
responding to.
Agreed, if you don't ask you might never know.
Because the Standard says so.
Robert's answer is correct, but unsatisfactory. The
obvious follow-up is "But *why* does the Standard say so?" [snip]
Second, the very notion of "the size" of a function is
rather slippery. What if the function has been expanded in-
in five or six places? Is "the size" the total size of all
expansions? If so, it might not be a compile-time constant
if the function is inlined in different translation units.
If "the size" is the memory footprint of one expansion, which
one is it? Note that different in-line expansions may well
generate different instruction counts. What if the optimizer
intermixes the in-line expansion's instructions with those of
the caller? What if the optimizer produces some instructions
that are not strictly attributable to either the caller or
to the expanded function? What if some optimizations occur
at run-time (think "JIT compiler"), using profiling data
gathered during the run, data that might vary from one run
to the next?
Robert's answer is correct, but unsatisfactory. The
obvious follow-up is "But *why* does the Standard say so?"
There's no 100% certain answer [snip]
my answer is really just speculation.
maadhuu said:does it make sense to find the size of a function ???
something like sizeof(main) ???
Robert said:Robert's answer is correct, but unsatisfactory. The
obvious follow-up is "But *why* does the Standard say so?"
[...]
my answer is really just speculation.
So you think that speculating about the answer to a question related to
the one that was asked (for which you claim there is no definitive
answer) makes your answer any more satisfactory than mine?
> You can't malloc() memory and copy a function
> into (or from!) it, because functons are not data: C is
> specified in such a way that it can be implemented even if
> instructions and data occupy completely different address
> spaces, perhaps with completely different characteristics.
No.
gcc without --pedantic allows it, but the size it returns is 1
(at least for my test cases). With --pedantic, or if I
use a stricter compiler, it complains at compile time.
Remember, there isn't
anything you can -do- to a function pointer,
except to store it or call through it (or convert it to void*). sizeof a
function might make sense if you could index the function object in some
way, but you cannot (not without invoking undefined behaviour.)
Lawrence Kirby said:That's not unreasonable. Remember that sizeof's result is in units of the
type being pointed at. So if you take the difference between 2 pointers
to int, you will get a count of ints, not bytes. So takeing the difference
between 2 function pointers would at best give you a count in units of
functions. In practice however sizeof is only defined over arrays i.e.
collections of things that have the same size and doesn't make any sense
for functions.
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.