本文介绍了自动变量在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下代码中:


int i = 5; ---它去.data段

int j; ---它去了bss段

int main()

{

int c;

int i = 5; --- stack

int j [5] = new int [5]; ----堆

c = i * 2; ----转到.text段


}

我的问题是:当创建目标文件时,有文本,数据

和bss段等...但是有没有像堆栈和堆那样的
段,这些自动变量会发生什么?


我希望我我有意义.....

Siddharth

In the following code:

int i = 5; ---it goes to .data segment
int j; ---it goes to bss segment
int main()
{
int c;
int i = 5; ---stack
int j[5] = new int[5]; ----heap
c = i*2; ----goes to .text segment

}
My question is : When the object file is created there are text, data
and bss segments etc...but there is notthing like stack and heap
segment, what happens to these automatic variables ?

I hope I am making sense.....
Siddharth

推荐答案



C标准并不保证这一点。它甚至不要求

实现识别.data或bss段的概念。

The C Standard doesn''t guarantee this. Nor does it even require that
implementations recognise the concept of .data or bss segments.



C标准并不保证这一点。它甚至不要求

实现识别(硬件)堆栈的概念。

The C Standard doesn''t guarantee this. Nor does it even require that
implementations recognise the concept of a (hardware) stack.



在C中,这只是一个语法错误。


如果您对C ++有疑问语言,在comp.lang.c ++中询问。

如果您对

实现使用的存储技术有疑问,请在致力于该实现的小组中询问。 />

< snip>


-

Richard Heathfield< http://www.cpax.org。英国>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

In C, this is just a syntax error.

If you have questions about the C++ language, ask in comp.lang.c++.
If you have questions about the storage techniques used by your
implementation, ask in a group devoted to that implementation.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999




不一定。

Not necessarily.



不一定。

Not necessarily.



不一定。

Not necessarily.



这是C中的语法错误。如果您在做C ++时注意到有一个

专用组它:comp.lang.c ++。

This is a syntax error in C. If your doing C++ the note that there is a
specialised group for it: comp.lang.c++.



不一定。

Not necessarily.



首先,就标准C而言,根本不需要任何类型的b / b
。实际上,最后一个转换阶段产生的最终可执行映像

的格式根本没有指定。在

的事实上,一个从不生产机器语言

表示的翻译也是完全合法的。


您提到的细分市场是一个普通的系统来布局

可执行文件,但这不是通用的,即使在整个方案中,也存在很多细节上的变化。 />

一般情况下,自动对象被放置在某种堆栈

之类中。内存(在大多数实现中恰好是实际的
硬件辅助堆栈),而静态对象驻留在其他地方。

编译器也可能决定将字符串文字和const限定的

对象放在只读存储中,而

malloc / calloc返回的内存/ realloc通常来自所谓的堆,尽管

再次强调C本身不需要这样的分类,尽管它是常用的。


例如,决定将所有对象放在

堆上的C实现完全符合,而决定分配所有对象的C实现
堆栈上的
对象也完全符合。


为了得到更满意的答案(虽然它不是通用答案),你

必须检查实现的细节,操作系统使用的内存模型,b / b
实现产生的可执行文件的格式等。汇编语言组可能是a

更好的主意,因为这些类型的机器级细节经常是在那里发了

。如果您的系统是基于x86的系统,请参阅comp.lang.asm.x86,对于其他体系结构,请参阅
或alt.lang.asm。如果您的系统是嵌入式的

,请参阅comp.arch.embedded。

Firstly, as far as Standard C is concerned, there need be no segments of
any kind at all. In fact, the format of the final executable image
produced by the last translation phase is not specified at all. In
fact, an interpreter that never produces a machine language
representation is also perfectly legal.

The segments you have mentioned are a common system of laying out
executable files, but this is not universal, and even within the
overall scheme, there is a lot of variation in detail.

In general though, automatic objects are placed in some kind of "stack
like" memory (which under most implementations happens to be an actual
hardware assisted stack), while static objects reside elsewhere. The
compiler may also decide to place string literals and const qualified
objects in read-only storage, while memory returned by
malloc/calloc/realloc is usually from the so-called "heap", though
again it must be emphasised that C itself does not require such
classification, though it is commonly used.

For example a C implementation that decides to place all objects on the
heap is perfectly conforming, while one that decides to allocate all
objects on a stack is also perfectly conforming.

To get a more satisfactory answer (though it is not a generic one), you
must examine the details of your implementation, the memory model used
by your operating system, the format of the executable that your
implementation produces etc. An assembly language group might be a
better idea since these sorts of machine level details are often dealt
with there. See comp.lang.asm.x86 if your system is an x86 based one,
or alt.lang.asm for other architectures. If your system is an embedded
one see comp.arch.embedded.




C标准并不保证这一点。它甚至不要求

实现识别.data或bss段的概念。


The C Standard doesn''t guarantee this. Nor does it even require that
implementations recognise the concept of .data or bss segments.



C标准并不保证这一点。它甚至不要求

实现识别(硬件)堆栈的概念。


The C Standard doesn''t guarantee this. Nor does it even require that
implementations recognise the concept of a (hardware) stack.



在C中,这只是一个语法错误。


如果您对C ++有疑问语言,在comp.lang.c ++中询问。

如果您对

实现使用的存储技术有疑问,请在致力于该实现的小组中询问。 />

< snip>


-

Richard Heathfield< http://www.cpax.org。英国>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日


In C, this is just a syntax error.

If you have questions about the C++ language, ask in comp.lang.c++.
If you have questions about the storage techniques used by your
implementation, ask in a group devoted to that implementation.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999



有人可以回答这个问题,假设它是在一个
linux m / c上运行,可执行文件是a.out。

Can someone please answer the question assuming that it was run on a
linux m/c and the executable was a.out.


这篇关于自动变量在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 13:35