imanish11111 wrote: | thanx sarath , i kno this answer .. i am asking WHY we cant use function name .....
|
|
For one, it's one of those things that's just outside the scope of the language. Since no deliberate language construct is designed to allow memory operations on functions, there would be no need to allow it. Furthermore, the result of sizeof on a function wouldn't be very accurate, given that it may or may not be inlined, and it may or may not be optimized away completely. In case of the inline, should it return 0 (since there's really no standalone function anymore), or should it return the size of the inlined code
There may also be other practical issues, such as when the sizeof is expanded during a compile. I'm (sadly) not familiar with the exact order of events in that case, but I imagine that the worst case would be if code optimization happens after the expansion of sizeof. In that case, the result of the operator could (and would) be completely inaccurate for most functions.
One workaround, to call it that, is to wrap a function with some piece of information which lets you otherwise extract its approximate size, such as:
static void __declspec(noinline) myFunction(void* p) { // do stuff } #pragma optimize("", off) static void dummy_end_function(){} #pragma optimize("", on)
Then extract the size such as
DWORD dwFuncSize = (DWORD)&dummy_end_function - (DWORD)&myFunction;
Notice however that I forcefully override inlining for myFunction, and also turn off optimizations for dummy_end_function. The latter is to make sure that it isn't discareded completely, if the compiler somehow rules it unimportant (even with the reference for dwFuncSize).
|