地址(安培)给出了编译器生成的地址或加载生成的地址? [英] Address of (&) gives compiler generated address or loader generated address?

查看:115
本文介绍了地址(安培)给出了编译器生成的地址或加载生成的地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int a;
printf("address is %u", &a);

这地址是这个..?我的意思是,这是一个编译器生成的地址,即虚拟地址或RAM装载机给定物理地址..?

Which address is this..? I mean is this a compiler generated address i.e. virtual address or the loader given physical address in the RAM..?

由于每一次打印不同的地址,我想这一定是在RAM地址。只是想确认一下。

As it prints different address every time, I guess it must be address in the RAM. Just want to make sure.

请提供这在你的答案提供参考的任何链接。

Please provide any links which give reference to your answer.

推荐答案

正确的答案是:这取决于

The correct answer is: "it depends."

(printf的本应使用%P指令,投地址显示为无效*,对于定义良好的烦躁的缘故:

(The printf should use the "%p" directive, and cast the address to "void *", for the sake of well-defined-ness:

printf("%p\n", (void *)&a);

虽然使用%u无疑适用于您与您正在使用的任何标记特定的编译器。)

although using %u no doubt works for your particular compiler with whatever flags you are using.)

由于@Alex指出,如果翻译是怎么回事地址是虚拟的(与最现代的操作系统,甚至在运行时,虚拟机下的模拟物理)。地址本身如果不是通常是在当一具有静态存储时间链接或加载时间确定的,但在运行时(堆栈作为@Als上说的)。申报的静态或外部变量有静态的持续时间;外函数体中声明的变量有静态的持续时间;和函数体中声明的变量,但不使用任何外部或静态的,具有自动存储时间(以及因此通常在堆栈 - 虽然可以有多个堆栈,使用POSIX线程时)。

As @Alex noted, the address is virtual if translation is going on (as with most modern OSes, or even when running in "emulated physical" under a virtual machine). The address itself is generally determined at link or load time if "a" has static storage duration, but at runtime (on the stack as @Als said) if not. Variables declared "static" or "extern" have static duration; variables declared outside function bodies have static duration; and variables declared within function bodies, but without using either "extern" or "static", have automatic storage duration (and are thus usually on "the stack"—though there can be more than one stack, as when using POSIX threads).

这篇关于地址(安培)给出了编译器生成的地址或加载生成的地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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