The special thing about function type is that it is not used as proper value -- in C you cannot define a variable of function type (only a pointer). There is no meaning in sizeof(*foo), sizeof(bar), bar is practically the same as &bar since the only thing that can be done with bar is to call it (that is set up for function call, and perform a call to the address where bar resides).Creature wrote:Don't you meanearlz wrote:Code: Select all
typedef void (*func)(int param); ... func foo; void bar(int p){ } ... foo=bar;
or am I missing something?Code: Select all
foo = &bar;
function lenght in C
Re: function lenght in C
Re: function lenght in C
You don't need to, but since it's a function POINTER, you usually use the memory address of a function because it makes things more clear that way. Either way, with or without the memory address, the function does the same.earlz wrote:func==&func... I mean, how would a compiler handle dereferencing a function pointer?Creature wrote:Don't you meanearlz wrote:Code: Select all
typedef void (*func)(int param); ... func foo; void bar(int p){ } ... foo=bar;
or am I missing something?Code: Select all
foo = &bar;
This:
Code: Select all
void (*p) (void) = foo;
p();
Code: Select all
void (*p) (void) = &foo;
p();
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
Re: function lenght in C
And this:
Will have the exact same result as:
Not completely logical but it is unambiguous.
Roel
Code: Select all
p();
Code: Select all
(*p)();
Roel
- Love4Boobies
- Member
- Posts: 2111
- Joined: Fri Mar 07, 2008 5:36 pm
- Location: Bucharest, Romania
Re: function lenght in C
You can get the size of a function in just one pass -- at runtime. Just define a label at the end of the function and substract the pointer to the function from that.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
[ Project UDI ]
Re: function lenght in C
This is invalid for several reasons:Love4Boobies wrote:You can get the size of a function in just one pass -- at runtime. Just define a label at the end of the function and substract the pointer to the function from that.
1) You cannot define labels in runtime.
2) You cannot take the address of a label in standard C.
3) The function need not be stored in consecutive memory addresses (the end of the function may be right before the entry point).
- Love4Boobies
- Member
- Posts: 2111
- Joined: Fri Mar 07, 2008 5:36 pm
- Location: Bucharest, Romania
Re: function lenght in C
1) You don't define them at runtime. Obviously the code is always the same size. I'm just saying that you need to do the substraction at runtime.skyking wrote:This is invalid for several reasons:Love4Boobies wrote:You can get the size of a function in just one pass -- at runtime. Just define a label at the end of the function and substract the pointer to the function from that.
1) You cannot define labels in runtime.
2) You cannot take the address of a label in standard C.
3) The function need not be stored in consecutive memory addresses (the end of the function may be right before the entry point).
2) Oh, come on. I bet you can be more resourceful than that.
3) Are you even talking about C functions anymore?
And if you really can't think of anything else, you can just check the damned assembly or object file.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
[ Project UDI ]
Re: function lenght in C
Sorry that I interpreted your post litteralyLove4Boobies wrote:1) You don't define them at runtime. Obviously the code is always the same size. I'm just saying that you need to do the substraction at runtime.skyking wrote:This is invalid for several reasons:Love4Boobies wrote:You can get the size of a function in just one pass -- at runtime. Just define a label at the end of the function and substract the pointer to the function from that.
1) You cannot define labels in runtime.
2) You cannot take the address of a label in standard C.
3) The function need not be stored in consecutive memory addresses (the end of the function may be right before the entry point).
And if it's a symbol placed after the function it does need to be placed there in the output.2) Oh, come on. I bet you can be more resourceful than that.
Yes that was the problem in the original post. C does not have the notation of function size for different reasons. C functions does not need to compile to continuos code blocks, it's for example possible that two functions share code fragments.3) Are you even talking about C functions anymore?
- Love4Boobies
- Member
- Posts: 2111
- Joined: Fri Mar 07, 2008 5:36 pm
- Location: Bucharest, Romania
Re: function lenght in C
I wasn't aware of this memory optimization technique (and have never seen it while playing around with disassembled code; then again, I usually optimize for speed). One reason I can think of why you can't have a keyword similar to sizeof is that sizeof gets replaced with a constant during the preprocessing phase. You can't know the size of a function during preprocessing because (a) the code generator didn't generate anything (b) even if you had some way of figuring out the size without having the code generator do its thing (although I'm not sure how) there are issues concerning optimization levels and the use of smart linking.skyking wrote:C functions does not need to compile to continuos code blocks, it's for example possible that two functions share code fragments.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
[ Project UDI ]
Re: function lenght in C
The bottom line of it all is, you can hack your way around this problem somehow, on the assembly / object code level, so that it works on a given platform for a given toolchain. But it will be a hack, not something done "in C".
Every good solution is obvious once you've found it.