www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - function pointers in D2

reply Nrgyzer <nrgyzer gmail.com> writes:
Hey guys,

I've used D1 in the past and D1 seems to check for correct function
pointers. In D2 I can pass any function pointer to an function, for
example:

...
void example(void function(int, int) fp) {
...
}

...
void callback1(int x, int y) {
}
void callback2() {
}
...

example(&callback1); // Works in D1 and D2
example(&callback2); // Works in D2, but D1 says that example needs
an pointer (int, int) signature which is logical. D1 says "... (void
function(int, int)) does not match parameter types void function())"

But... why does D2 accept callback2? When I compile an application by
using debug mode in D2, the callback works... in release mode it
produces an "object.Error: Access Violation".

Is there any chance to check for correct function pointers in D2?
Dec 12 2010
parent reply Christopher Nicholson-Sauls <ibisbasenji gmail.com> writes:
On 12/12/10 03:54, Nrgyzer wrote:
 Hey guys,
 
 I've used D1 in the past and D1 seems to check for correct function
 pointers. In D2 I can pass any function pointer to an function, for
 example:
 
 ...
 void example(void function(int, int) fp) {
 ...
 }
 
 ...
 void callback1(int x, int y) {
 }
 void callback2() {
 }
 ...
 
 example(&callback1); // Works in D1 and D2
 example(&callback2); // Works in D2, but D1 says that example needs
 an pointer (int, int) signature which is logical. D1 says "... (void
 function(int, int)) does not match parameter types void function())"
 
 But... why does D2 accept callback2? When I compile an application by
 using debug mode in D2, the callback works... in release mode it
 produces an "object.Error: Access Violation".
 
 Is there any chance to check for correct function pointers in D2?

Confirmed behavior with DMD 2.050 on Linux. Somewhat more telling example: -------------------------------------------------- import std.stdio; void example ( void function( int, int ) fp ) { writef( "fp( %s, %s ) == ", 5, 10 ); fp( 5, 10 ); } void callback1 ( int x, int y ) { writefln( "callback1( %s, %s )", x, y ); } void callback2 ( int z ) { writefln( "callback2( %s )", z ); } void callback3 () { writeln( "callback3()" ); } void main () { example( &callback1 ); example( &callback2 ); example( &callback3 ); } -------------------------------------------------- Compiles clean and outputs: fp( 5, 10 ) == callback1( 5, 10 ) fp( 5, 10 ) == callback2( 10 ) fp( 5, 10 ) == callback3() Which says to me that the arguments are going to the stack even when unneeded. It appears DMD is failing to check the pointer type? This surprises me, and certainly has to be a bug. -- Chris N-S
Dec 12 2010
parent "Daniel Murphy" <yebblies nospamgmail.com> writes:
"Christopher Nicholson-Sauls" <ibisbasenji gmail.com> wrote in message 
news:ie28aa$18bn$1 digitalmars.com...
  This
 surprises me, and certainly has to be a bug.

http://d.puremagic.com/issues/show_bug.cgi?id=3797
Dec 12 2010