sizeof(main)  
Author Message
imanish11111





PostPosted: Visual C++ Language, sizeof(main) Top

hi,
i searched a lot to find the answer for this question before posting.... i am not getting proper answers.
. my questoin is wat is
sizeof(main)

alot of ppl say dat sizeof doesn't take function name ...
.. but a fucntion name is a pointer like .. cout<<func; wiil print the address this means that its a fucntion pointer like object..... so the sizeof a function name shud equal to sizeof a pointer variable.....
. . my question is why it does.nt take a fucntion name....


plz reply soon
manish.............






Visual C++6  
 
 
Sarath.





PostPosted: Visual C++ Language, sizeof(main) Top

From MSDN:

The sizeof operator cannot be used with the following operands:

  • Functions. (However, sizeof can be applied to pointers to functions.)
  • Bit fields.
  • Undefined classes.
  • The type void.
  • Dynamically allocated arrays.
  • External arrays.
  • Incomplete types.
  • Parenthesized names of incomplete types



 
 
imanish11111





PostPosted: Visual C++ Language, sizeof(main) Top

thanx sarath , i kno this answer .. i am asking WHY we cant use function name .....

 
 
einaros





PostPosted: Visual C++ Language, sizeof(main) Top

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).



 
 
Viorel.





PostPosted: Visual C++ Language, sizeof(main) Top

 
[...] so the sizeof a function name shud equal to sizeof a pointer variable. [...]

Note that sizeof(&main) with address operator seems to be an acceptable expression (works in Visual Studio 2005) and returns 4, i.e. the size of a pointer.

 


 
 
Sarath.





PostPosted: Visual C++ Language, sizeof(main) Top

Viorel,

The expression will be evaluates successfully and will return 4 in Visual Studio 6.0 too.
His questions was sizeof(main); which could not be evaluated and Einar also made some good points on it.