Is there a way to find the sizeof a function, I haven't tried, but I would think sizeof (printf("hi")); would return the size of what printf returned, an int.
Is there a way to find the sizeof a function, I haven't tried, but I would think sizeof (printf("hi")); would return the size of what printf returned, an int.
Help populate a c/c++ help irc channel
server: irc://irc.efnet.net
channel: #c
It isn't a function. It's an operator.
Quzah.
Hope is the first step on the road to disappointment.
I know sizeof is an operator, I want to know if there is a way to find how much space a function is taking up in memory, if any at all.
Help populate a c/c++ help irc channel
server: irc://irc.efnet.net
channel: #c
>Is there a way to find the sizeof a function
No. The first thing the C standard says about sizeof in section 6.5.3.4 is that it "shall not be applied to an expression that has function type or an incomplete type, to the parenthesized name of such a type, or to an expresison that designates a bit-field member."
My best code is written with the delete key.
If the linker has been kind enough to lay the functions out in order. What do you need it for?Code:void Func1(void) { ... } void Func2(void) { ... } size_t cbFunc1 = (char *) Func2 - (char *) Func1;
As mentioned in the thread on pointer math, this is undefined behaviour, since you're not addressing members of an array (or the space one beyond said array).Originally posted by anonytmouse
If the linker has been kind enough to lay the functions out in order. What do you need it for?Code:size_t cbFunc1 = (char *) Func2 - (char *) Func1;
Quzah.
Hope is the first step on the road to disappointment.
You can't calculate the 'size of' a function in C. First of all, there is no built-in facility to do it. And since C is a hi-level language, each compiler will generate different code because of it's own idiosyncracies, not to mention the specific requirements of each individual platform and CPU. Using assembly you could do it quite easily, of course.
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
So can we legally avoid the pointer maths?As mentioned in the thread on pointer math, this is undefined behaviour, since you're not addressing members of an array (or the space one beyond said array).
Anyway, "undefined" doesn't mean "never works", both examples work on MSVC. Of course, changing compilers or even changing linker options is likely to break this method. However, once the code is compiled, this method should work (or not work) reliably.Code:size_t cbFunc1 = (size_t) Func2 - (size_t) Func1;
Could I ask how?Using assembly you could do it quite easily, of course.
P.S
LCC-WIN32 chokes on both of these examples with an internal error!Of course, changing compilers or even changing linker options is likely to break this method.
Thanks.
I'm not too knowledgable on assembly. Does that mean the function must be assembly also or is there a way to figure out the size of a C function reliably with assembly?
EDIT: What the!!!!!
>> Could I ask how?
Code:.data fmt db "Size of 'myfunc': %d", 0x0d, 0x0a, 0x00 size dd 0x00000000 .code start: jmp > calc myfunc: xor eax, eax mov eax, 0 ret boundary: calc: mov eax, boundary - myfunc; <--- subtract labels mov [size], eax push [size] push offset fmt call printf add esp, 8 xor eax, eax ret
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
Sorry, I was trying to make the code more readable.
>> Does that mean the function must be assembly also or is there a way to figure out the size of a C function reliably with assembly?
That's really two separate questions.
>> Does that mean the function must be assembly...
A procedure is a procedure. It begins at some offset and ends with some sort of ret.
>> is there a way to figure out the size of a C function reliably with assembly
Sure. Dissasemble the code and do the measuring manually.
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }