_ReturnAddress (C++)

这篇博客介绍了_ReturnAddress内联函数,它提供调用函数返回后将执行的指令地址。文章通过构建示例程序并使用调试器进行步进,展示如何验证_ReturnAddress返回的地址与实际执行的下一条指令地址相符。同时,指出了优化如内联可能影响返回地址,并讨论了在/CLR编译环境下使用_ReturnAddress的注意事项。
导读:
 
Visual C++ Language Reference

_ReturnAddress









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


Build the following program and step through it in the debugger. As you step through the program, note the address that is returned from _ReturnAddress. Then, immediately after returning from the function where _ReturnAddress was used, open the How to: Use the Disassembly Window and note that the address of the next instruction to be executed matches the address returned from _ReturnAddress.


Optimizations such as inlining may affect the return address. For example, if the sample program below is compiled with /Ob1, inline_func will be inlined into the calling function, main. Therefore, the calls to _ReturnAddress from inline_func and main will each produce the same value.


When _ReturnAddress is used in a program compiled with /clr, the function containing the _ReturnAddress call will be compiled as a native function. When a function compiled as managed calls into the function containing _ReturnAddress, _ReturnAddress may not behave as expected.






Header file







// compiler_intrinsics__ReturnAddress.cpp

#include
#include

#pragma intrinsic(_ReturnAddress)

__declspec(noinline)
void noinline_func(void)
{
printf("Return address from %s: %p/n", __FUNCTION__, _ReturnAddress());
}

__forceinline
void inline_func(void)
{
printf("Return address from %s: %p/n", __FUNCTION__, _ReturnAddress());
}

int main(void)
{
noinline_func();
inline_func();
printf("Return address from %s: %p/n", __FUNCTION__, _ReturnAddress());

return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值