Hi, I need to know how (if it is possible) to determine the sizeof a function...

#include <iostream>
using namespace std;

int theFunction();
int theFunction()
{
    int x = 0;
    return x;
}

int main()
{
    cout << sizeof theFunction;
    cin.get();
    return 0;
}

The above does not work.
How would I make it work?

I am trying to output the raw code for the function to a file. so, if retrieving the size of the function is unnecessary, please tell me.

Thanks in advance.

Recommended Answers

All 9 Replies

There is no portable way to do it because the number of bytes occupied by a function may vary by compiler and even within the same compiler from one compile to another. Most compilers will generate the assembly code for a program.

I am trying to output the raw code for the function to a file. so, if retrieving the size of the function is unnecessary, please tell me.

Since you did not mention it, I take it there is no way to do that?

what if I did something like this:

int theFunction()
{
    //Bla blah blah, code here.
    //More code.
    int EndMarker;
    return 0;
}

would the address of EndMarker not be essentially the end of theFunction? Excluding the return, of course.

Since you did not mention it, I take it there is no way to do that?

I did mention it. If you want a YES or a NO, then the answer must be NO. If you think about it for a little while the sizeof is an operator, not a function, and is evaluated at compile time. And at compile time the final code for the target function hasn't even been generated. So how is the sizeof operator supposed to evaluate something that doesn't exist yet?

what if I did something like this:

int theFunction()
{
    //Bla blah blah, code here.
    //More code.
    int EndMarker;
    return 0;
}

would the address of EndMarker not be essentially the end of theFunction? Excluding the return, of course.

No -- the address of EndMarker is not even within the address space of the function. Its address is on the stack and the function code resides in the code segment(s).

If you think about it for a little while the sizeof is an operator, not a function, and is evaluated at compile time. And at compile time the final code for the target function hasn't even been generated. So how is the sizeof operator supposed to evaluate something that doesn't exist yet?

Yeah... I realize that now.
My previous question about the "EndMarker" still stands though.

:( Well, that sucks.
Thanks!

Okay, one more try:

void theFunction1();
void theFunction2();

void theFunction1()
{
   //blah blah blah
}

void theFunction2()
{
   //blah blah blah
}

what if I took the difference in the addresses of theFunction1, and theFunction2?
Are they stored sequentially like that?

I think by this point AD is tired of answering the same question the same way again and again and again...

If you want the machine code for the function, tell your compiler to output the assembly code, chop off everything except the function, and run it through your assembler, or plug the opcodes directly into DEBUG (and the DOS prompt) and save the codes to file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.