在printf的C程序的当前地址 [英] Printf the current address in C program

查看:126
本文介绍了在printf的C程序的当前地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下,我有以下简单的C程序:

Imagine I have the following simple C program:

int main() {

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

现在,我想知道前任pression的地址C = A + B,即程序地址
其中,此加法进行。是否有可能,我可以用printf?
沿线的东西:

Now, I would like to know the address of the expression c=a+b, that is the program address where this addition is carried out. Is there any possibility that I could use printf? Something along the line:

int main() {

int a=5, b= 6, c;
printf("Address of printf instruction in memory: %x", current_address_pointer_or_something)
c = a +b; 
return 0;
}

我知道我怎么能使用GDB,然后信息行file.c中找到的地址了:行。然而,我知道,如果我还可以借助printf直接做到这一点。

I know how I could find the address out by using gdb and then info line file.c:line. However, I should know if I could also do that directly with the printf.

推荐答案

在GCC,你可以使用&放取标号的地址;&安培;运营商。所以,你可以这样做:

In gcc, you can take the address of a label using the && operator. So you could do this:

int main() 
{
    int a=5, b= 6, c;

    sum:
        c = a+b;

    printf("Address of sum label in memory: %p", &&sum);
    return 0;
}

&安培的结果;&安培;总结是,如果你做了一个转到款项,将被发射的跳转指令的目标。所以,虽然这是真的,有一个在C / C ++没有一个人对一地址到在线地图,你还可以说给我一个指向此code。

The result of &&sum is the target of the jump instruction that would be emitted if you did a goto sum. So, while it's true that there's no one-to-one address-to-line mapping in C/C++, you can still say "get me a pointer to this code."

这篇关于在printf的C程序的当前地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆