Page 3 of 3

Re: function lenght in C

Posted: Wed Apr 22, 2009 1:37 am
by skyking
Creature wrote:
earlz wrote:

Code: Select all

typedef void (*func)(int param);
...

func foo;
void bar(int p){
}
...
foo=bar;
Don't you mean

Code: Select all

foo = &bar;
or am I missing something?
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).

Re: function lenght in C

Posted: Wed Apr 22, 2009 4:51 am
by Creature
earlz wrote:
Creature wrote:
earlz wrote:

Code: Select all

typedef void (*func)(int param);
...

func foo;
void bar(int p){
}
...
foo=bar;
Don't you mean

Code: Select all

foo = &bar;
or am I missing something?
func==&func... I mean, how would a compiler handle dereferencing a function pointer?
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.

This:

Code: Select all

void (*p) (void) = foo;

p();
Will have the exact same result as:

Code: Select all

void (*p) (void) = &foo;

p();
At least in C++, but I'm assuming it's the same in C, as it originates from C. Either way, sorry for bringing this up, didn't check my stuff and thought that without the '&' it wouldn't work. My mistake.

Re: function lenght in C

Posted: Wed Apr 22, 2009 7:56 am
by qw
And this:

Code: Select all

p();
Will have the exact same result as:

Code: Select all

(*p)();
Not completely logical but it is unambiguous.

Roel

Re: function lenght in C

Posted: Wed Apr 22, 2009 9:09 am
by Love4Boobies
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.

Re: function lenght in C

Posted: Wed Apr 22, 2009 9:49 am
by skyking
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.
This is invalid for several reasons:
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).

Re: function lenght in C

Posted: Wed Apr 22, 2009 7:25 pm
by Love4Boobies
skyking wrote:
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.
This is invalid for several reasons:
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).
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.
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.

Re: function lenght in C

Posted: Thu Apr 23, 2009 12:03 am
by skyking
Love4Boobies wrote:
skyking wrote:
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.
This is invalid for several reasons:
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).
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.
Sorry that I interpreted your post litteraly :twisted:
2) Oh, come on. I bet you can be more resourceful than that.
And if it's a symbol placed after the function it does need to be placed there in the output.
3) Are you even talking about C functions anymore?
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.

Re: function lenght in C

Posted: Thu Apr 23, 2009 12:14 am
by Love4Boobies
skyking wrote:C functions does not need to compile to continuos code blocks, it's for example possible that two functions share code fragments.
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.

Re: function lenght in C

Posted: Thu Apr 23, 2009 12:27 am
by Solar
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".