get size of function

Johannes Pfau spam at example.com
Wed Aug 31 01:26:30 PDT 2011


maarten van damme wrote:
>substracting the pointers to two sequential functions didn't work out.
>the following snippet:
>//
>auto first=function void(){
>asm{
>naked;
>nop;
>}
>};
>auto next=function void(){
>asm{
>naked;
>nop;
>}
>};
>writeln(cast(int)&next-cast(int)&first,"/",cast(int)&next,"/",&next,"/",cast(int)&first,"/",&first);
>//
>outputs
>4/1244672/12FE00/1244668/12FDFC
>so the conversions from hex to int work correctly and I still get a
>size of 4 while it shouldve been a size of 1.
>Are there other options?
>
Do you need to use function _pointers_?
In your example next & first are function pointers and function
pointers have a size of 4 (on a 32bit system).

This seems to work:
-----------------------------
int first(int a, int b)
{
    return a - b;
}

int next(int a, int b)
{
    return a + b;
}

void main(string args[])
{
    writeln(cast(int)&next-cast(int)&first,"/",cast(int)&next,"/",&next,"/",cast(int)&first,"/",&first);
}
-----------------------------
Output: 20/134772104/8087588/134772084/8087574

As next and first in your example should already be pointers, you
could also try to change your writeln to this:
writeln(cast(int)next-cast(int)first,"/",cast(int)next,"/",next,"/",cast(int)first,"/",first);

BTW: Why not cast to void* (or at least size_t) instead of int?

-- 
Johannes Pfau



More information about the Digitalmars-d-learn mailing list