本文介绍了运行时与编译时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解运行时环境。假设我有一个具有简单变量alpha的程序

。当这个变量是静态分配的
时,编译器可以使用alpha的绝对地址来对它进行
访问。令我困惑的是,当变量动态分配时,编译器如何实现它?我们知道

变量的地址,直到运行时。在编译过程中,我们怎样才能访问

alpha变量,因为我们还不知道它的地址呢?


提前致谢

I have hard time to understand run-time environment. Let assume that I have
a program that has a simple variable alpha. When this variable is
statically allocated, the compiler can use the absolute address of alpha to
access to it. What confuses me is that when the variable is dynamically
allocated, how does the compiler implement it? We know the address of the
variable until run-time. During the compilation, how can we access to the
alpha variable since we don''t know its address yet?

Thanks in advance

推荐答案





您对静态分配的假设是假设的。变量不正确。

编译器不知道内存中将加载整个程序的位置,所以

no绝对地址可以给出。


如何处理变量在很大程度上取决于它的声明位置,而不仅仅取决于是否使用new来为它分配空间。


例如,可能通过使用堆栈中的偏移量

来解决局部变量。指针。但由于没有实际要求存在堆栈的存在。在C ++语言规范中,编译器编写者

在技术上可以自由地实现他们认为合适的细节。


要回答关于动态分配变量的问题,你是
描述的是使用指针变量和函数new(或malloc

或alloc,但让我们坚持使用new,好吗?) 。指针变量就像

任何其他变量一样。如果它是一个本地指针,那么它可以作为上面描述的
(使用堆栈指针的偏移量)进行寻址。但是这个变量的

*内容*在分配它指向的

对象时会动态变化。当new被调用时,对象获取为其分配的空间,对象的构造函数被调用,并且

分配空间的地址被放入指针变量,作为它的*值*,而不是它的

位置!


编译器实际上没有安排任何内存,除了这些

我提到的本地堆栈偏移以及其他一些案例。链接器

和操作系统的加载机制一样起作用。如果你真的感兴趣的话,你可能想得到一本关于编译器理论的书。

它至少需要整整一个学期的研究大学。


-Howard




You assumption about the "statically allocated" variable is incorrect. The
compiler has no idea where in memory the entire program will be loaded, so
no "absolute address" can be given.

How a variable is addressed depends greatly on where it is declared, not
just on whether new was used to allocate space for it.

For example, a local variable is likely to be addressed by using an offset
from the "stack" pointer. But since there is no actual requirement for the
existence of a "stack" in the C++ language specification, compiler writers
are technically free to implement such details as they see fit.

To answer your question about dynamically allocated variables, what you''re
describing is the use of a pointer variable and the function new (or malloc
or alloc, but let''s stick with new, ok?). The pointer variable is just like
any other variable. If it''s a local pointer, then it may be addressed as I
described above (using an offset from the stack pointer). But it''s the
*contents* of that variable that get changed dynamically when allocating the
object it points to. An object gets space allocated for it when new is
called, the object''s constructor gets called, and the address of the
allocated space gets put into the pointer variable, as its *value*, not its
location!

The compiler doesn''t actually arrange any of the memory, outside of these
local Stack offsets I mentioned and perhaps a few other cases. The linker
comes into play, as does the operating system''s loading mechanism. You
might want to get a book on compiler theory if you''re really interested.
It''s at least a whole semester''s worth of study in college.

-Howard





希望有所帮助。

-JKop


Hope that helps.
-JKop


这篇关于运行时与编译时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 12:13