function lenght in C

Programming, for all ages and all languages.
skyking
Member
Member
Posts: 174
Joined: Sun Jan 06, 2008 8:41 am

Re: function lenght in C

Post 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).
User avatar
Creature
Member
Member
Posts: 548
Joined: Sat Dec 27, 2008 2:34 pm
Location: Belgium

Re: function lenght in C

Post 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.
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: function lenght in C

Post 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
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: function lenght in C

Post 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.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
skyking
Member
Member
Posts: 174
Joined: Sun Jan 06, 2008 8:41 am

Re: function lenght in C

Post 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).
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: function lenght in C

Post 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.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
skyking
Member
Member
Posts: 174
Joined: Sun Jan 06, 2008 8:41 am

Re: function lenght in C

Post 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.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: function lenght in C

Post 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.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: function lenght in C

Post 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".
Every good solution is obvious once you've found it.
Post Reply