views:

360

answers:

1

With msvc, is there an equivalent to gcc's "__builtin_return_address"?

I'm looking to find the address of the calling function, 1 level deep.

+8  A: 

__ReturnAddress

From MSDN:

The _ReturnAddress intrinsic provides the address of the instruction in the calling function that will be executed after control returns to the caller

Note that on some platforms, the result could be misleading due to tail folding - the compiler might have your inner function return 2 levels deep. This can commonly occur for code like this:

int DoSomething()
{
   return DoSomethingSpecial();
}

The compiler could generate code so DoSomethingSpecial returns directly to the caller of DoSomething.

Also, the return address is not trustworthy-enough to make security decisions, see here.

Michael