Bug 2312 – unexpected function addresses dump behaviour

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2008-08-25T07:29:00Z
Last change time
2014-03-01T00:35:57Z
Assigned to
bugzilla
Creator
enzo.petrelli

Comments

Comment #0 by enzo.petrelli — 2008-08-25T07:29:27Z
/*************************** OS: Vista SP1 Compiler/linker: Digital Mars D Compiler v1.034 Tango/tangobos Lib: tango-0.99.7-bin-win32-dmd.1.033 Compiled with: no compile/link flag During a test session where the dump of function addresses was required, an unexpected result was shown, as in the following simplified test code. The expected result was to see three equal series of three different addresses. It should also be noted that the requested padding with '0's for hex values in writefln does not work for void* ***************************/ import std.cstream; class DClass { void func1() { return; } void func2() { return; } void func3() { return; } } void main() { DClass oObj = new DClass; std.cstream.dout.writefln(" object: 0x%08X", cast(void*) &oObj); std.cstream.dout.writefln(); // case 1: std.cstream.dout.writefln("object.func1: 0x%08X", cast(void*) &oObj.func1); std.cstream.dout.writefln("object.func2: 0x%08X", cast(void*) &oObj.func2); std.cstream.dout.writefln("object.func3: 0x%08X", cast(void*) &oObj.func3); // case 2: alias void delegate() DlgPtr; DlgPtr pDlg = &oObj.func1; std.cstream.dout.writefln("object.pfunc1:0x%08X", cast(void*) pDlg); pDlg = &oObj.func2; std.cstream.dout.writefln("object.pfunc2:0x%08X", cast(void*) pDlg); pDlg = &oObj.func3; std.cstream.dout.writefln("object.pfunc3:0x%08X", cast(void*) pDlg); // case 3: alias void function() FncPtr; FncPtr pFnc = cast(FncPtr) &oObj.func1; std.cstream.dout.writefln("object.pfunc1:0x%08X", cast(void*) pFnc); pFnc = cast(FncPtr) &oObj.func2; std.cstream.dout.writefln("object.pfunc2:0x%08X", cast(void*) pFnc); pFnc = cast(FncPtr) &oObj.func3; std.cstream.dout.writefln("object.pfunc3:0x%08X", cast(void*) pFnc); } /* output obtained: object: 0x 12FE38 object.func1: 0x 19D3FF0 object.func2: 0x 19D3FF0 object.func3: 0x 19D3FF0 object.pfunc1:0x 19D3FF0 object.pfunc2:0x 19D3FF0 object.pfunc3:0x 19D3FF0 object.pfunc1:0x 402010 object.pfunc2:0x 402018 object.pfunc3:0x 402020 */
Comment #1 by bugzilla — 2008-08-26T01:18:06Z
Taking the address of a member function results in a delegate. Casting the delegate to a void* gives the value of the 'this' part of the delegate, which is oObj in the example, and always the same value. Casting the address of a member function directly to a function pointer gives the address of the function, hence the 3 different values in the last group. The hex formatting is still an issue, though.
Comment #2 by bugzilla — 2008-08-26T01:24:01Z
writefln doesn't format exactly like printf does. To get the desired hex result, cast to int, not void*.