digitalmars.D.learn - Are function pointers compile time constants?
- d coder (6/6) Feb 20 2011 Greetings
- Simon (7/13) Feb 20 2011 No a function doesn't have an address until the .exe is loaded into
- d coder (1/1) Feb 20 2011 Thanks Simon.
- Nick Sabalausky (5/17) Feb 20 2011 I didn't know Windows did that, I thought it was just certain versions o...
- Steven Schveighoffer (9/29) Feb 20 2011 Probably the first one with dlls? I don't see how else you could have
- Dan Olson (8/14) Feb 26 2011 I just want to point out that this *should* be doable in D. At compile
Greetings I tried to initialize a struct member with a function pointer, and found that DMD2 did not like it. Are not function pointers compile time constants? And why they should not be? Regards - Cherry
Feb 20 2011
On 20/02/2011 14:59, d coder wrote:Greetings I tried to initialize a struct member with a function pointer, and found that DMD2 did not like it. Are not function pointers compile time constants? And why they should not be? Regards - CherryNo a function doesn't have an address until the .exe is loaded into memory. And with Address space randomisation on 'doze there is no reasonable way to make a function pointer a compile time value. -- My enormous talent is exceeded only by my outrageous laziness. http://www.ssTk.co.uk
Feb 20 2011
"Simon" <s.d.hammett gmail.com> wrote in message news:ijrdif$1nn6$1 digitalmars.com...On 20/02/2011 14:59, d coder wrote:I didn't know Windows did that, I thought it was just certain versions of Unix/Linux. Do you happen to know which version of Windows was first to have it?Greetings I tried to initialize a struct member with a function pointer, and found that DMD2 did not like it. Are not function pointers compile time constants? And why they should not be? Regards - CherryNo a function doesn't have an address until the .exe is loaded into memory. And with Address space randomisation on 'doze there is no reasonable way to make a function pointer a compile time value.
Feb 20 2011
On Sun, 20 Feb 2011 16:23:14 -0500, Nick Sabalausky <a a.a> wrote:"Simon" <s.d.hammett gmail.com> wrote in message news:ijrdif$1nn6$1 digitalmars.com...Probably the first one with dlls? I don't see how else you could have dlls, because you can't just say "this function will always be at address 12345, and no other function anyone else ever compiles can take that spot". That being said, I'm not sure why the OP's issue couldn't be solved -- clearly position independent code works (and that is statically created), why couldn't a reference to that function also be created in a struct initializer? Is it a limitation of the linker? -SteveOn 20/02/2011 14:59, d coder wrote:I didn't know Windows did that, I thought it was just certain versions of Unix/Linux. Do you happen to know which version of Windows was first to have it?Greetings I tried to initialize a struct member with a function pointer, and found that DMD2 did not like it. Are not function pointers compile time constants? And why they should not be? Regards - CherryNo a function doesn't have an address until the .exe is loaded into memory. And with Address space randomisation on 'doze there is no reasonable way to make a function pointer a compile time value.
Feb 20 2011
On 21/02/2011 00:24, Steven Schveighoffer wrote:On Sun, 20 Feb 2011 16:23:14 -0500, Nick Sabalausky <a a.a> wrote:Vista. For any particular exe, you'll find that it's dependant dlls alway get loaded in the same place. That's why buffer overflow bugs (where/are) so easy to exploit."Simon" <s.d.hammett gmail.com> wrote in message news:ijrdif$1nn6$1 digitalmars.com...Probably the first one with dlls? I don't see how else you could have dlls, because you can't just say "this function will always be at address 12345, and no other function anyone else ever compiles can take that spot".On 20/02/2011 14:59, d coder wrote:I didn't know Windows did that, I thought it was just certain versions of Unix/Linux. Do you happen to know which version of Windows was first to have it?Greetings I tried to initialize a struct member with a function pointer, and found that DMD2 did not like it. Are not function pointers compile time constants? And why they should not be? Regards - CherryNo a function doesn't have an address until the .exe is loaded into memory. And with Address space randomisation on 'doze there is no reasonable way to make a function pointer a compile time value.That being said, I'm not sure why the OP's issue couldn't be solved -- clearly position independent code works (and that is statically created), why couldn't a reference to that function also be created in a struct initializer? Is it a limitation of the linker? -SteveYou can't use the relative jump of POS code. The offset to the function will be different at each call site. You could make function pointers compile time constants if: You disallow ASR You disallow them when compiling to a dll You disallow in-lining of any function of which you take the address You disallow the linker from rearranging functions You disallow the linker from merging duplicate functions. You merge the compiler and linker And it wouldn't work with template functions anyway as you can get multiple copies of those. That's a lot of stuff to give up. -- My enormous talent is exceeded only by my outrageous laziness. http://www.ssTk.co.uk
Feb 21 2011
Simon Wrote:You could make function pointers compile time constants if: You disallow ASR You disallow them when compiling to a dll You disallow in-lining of any function of which you take the address You disallow the linker from rearranging functions You disallow the linker from merging duplicate functions. You merge the compiler and linkerInterestingly, this data layout is impossible without rearranging: byte a; byte[&c] b; byte c;
Feb 22 2011
d coder <dlang.coder gmail.com> writes:Greetings I tried to initialize a struct member with a function pointer, and found that DMD2 did not like it. Are not function pointers compile time constants? And why they should not be? Regards - CherryI just want to point out that this *should* be doable in D. At compile time the function's address in the object code is a placeholder that the linker or loader will fixup based on the function symbol. This is what linkers do! And if an function ptr is used, the function can still be inlined; just need to keep around the real function reference through the pointer. Dan
Feb 26 2011