Printf the current address in C program

后端 未结 8 1331
时光说笑
时光说笑 2021-02-04 12:27

Imagine I have the following simple C program:

int main() {

int a=5, b= 6, c;
c = a +b; 
return 0;
}

Now, I would like to know the address of

8条回答
  •  一向
    一向 (楼主)
    2021-02-04 12:42

    Visual C++ has the _ReturnAddress intrinsic, which can be used to get some info here.

    For instance:

    __declspec(noinline) void PrintCurrentAddress()
    {
        printf("%p", __ReturnAddress);
    }
    

    Which will give you an address close to the expression you're looking at. In the event of some optimizations, like tail folding, this will not be reliable.

提交回复
热议问题