堆栈空间,全球空间,堆空间 [英] Stack space, global space, heap space

查看:73
本文介绍了堆栈空间,全球空间,堆空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候:


我知道在堆栈上声明的变量肯定不会存在于堆

空间中所以只有非常有限的东西你可以在一个函数的堆栈中存储

。但是全球空间呢?那堆

作为堆空间还是一种特殊的堆栈形式?因为曾经

我见过有人在全球空间声明一个1兆字节的字符数组,如下所示:


char s [1000000];


它有效。

问候,


硕翔

解决方案

Shuo Xiang写道:

我知道堆栈上声明的变量肯定不会驻留在堆空间中


你不能在堆栈上声明变量你可以做的最好的事情就是把它们作为[非静态]函数 - 本地人,这*允许*它们在

a堆栈上实现,但肯定没有不需要它。 [例如,他们可以在垃圾收集堆中实现
。这可能很疯狂,但它可能是
。]

因此只有非常有限的东西你可以存储在一堆栈中功能。


为什么?堆栈可以像实现一样大。小心建议我们

不要过头,因为我们知道一些流行的实现有点......

weedy ......但是(除非标准另有说明)这是

实现的属性,而不是语言的属性。

因为有一次我见过有人在全球空间声明一个1兆字节的char数组像这样:

char s [1000000];

它有效。




机器必须有足够的那个房间吧。


-

Chris" electric hedgehog" Dollin

C常见问题解答: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html

C欢迎: http://www.angelfire.com/ms3/bchambl...me_to_clc.html


Shuo Xiang写道:


问候:

我知道变量声明了堆栈绝对不存在于堆空间中,因此只有非常有限的东西可以存储在函数堆栈中。但是全球空间呢?这与堆空间是一样的还是仍然是特殊堆栈的形式?因为有一次
我看到有人在全球空间声明了一个1兆字节的char数组,如下所示:

char s [1000000];

它有效。




首先,C答案。 C没有堆叠,没有堆,并且没有

全球空间。 C支持具有三种

存储持续时间的数据对象。即自动,动态和静态。

创建这些对象的位置以及如何管理它们的生命周期的细节完全取决于实现;

C标准规定了对象的行为方式,但没有说明关于如何实现行为的
。实施可能(或

可能不会)对

三个不同存储持续时间的对象施加不同的大小限制;再一次,C标准是这样的限制可以采取什么样的形式。


其次,实际答案。许多C实现使用

a堆栈用于自动持续时间变量,并且许多实现

对堆栈驻留对象施加比对于

其他更严格的大小限制各种物体。这些限制的细节(包括

是否以及如何调整它们)是特定于实现的,

并且只能通过咨询实现来解决。 $>
文档和/或大师。


最后,请注意,C实现不需要是支持一个人的b
完全是兆字节数组,无论使用什么

存储持续时间。事实上,许多实现确实处理了这个尺寸和更大尺寸的物品,但标准并不需要

极简主义。 C能够这样做。


-
呃*********@sun.com




" Chris Dollin" <柯** @ hpl.hp.com>在消息中写道


为什么?堆栈可以像实现一样大。小心建议
我们不要过分,因为我们知道一些流行的实现是一个很小的... weedy ......但是(除非标准另有说明)这是一个
实现的属性,而不是语言的属性。



但是提供小堆栈有充分的理由。首先堆栈

使用率仅作为结构化程序大小的对数增加。

其次,大多数处理器实现缓存方案,如果堆栈很小

足以确保它始终保存在缓存中,可能会有b $ ba性能提升。第三,在编译时知道一个大的
数据项的大小是不常见的,并且99 C之前不允许可变大小的堆栈

数组。 />


Greetings:

I know that variables declared on a stack definitely does not reside in heap
space so there is only a very limited amount of stuff that you can store in
the stack of a function. But what about the global space? Is that the same
as the heap space or is that still a form of special stack? Because once
I''ve seen someone declare a 1 megabyte char array in global space like this:

char s[1000000];

and it worked.
Regards,

Shuo Xiang

解决方案

Shuo Xiang wrote:

I know that variables declared on a stack definitely does not reside in
heap space
You can''t declare variables "on a stack" in C. The best you can do is make
them [non-static] function-locals, which *allows* them to be implemented on
a stack but certainly doesn''t require it. [They could, for example, be
implemented in a garbage-collected heap. This might be mad, but it is
possible.]
so there is only a very limited amount of stuff that you can
store in the stack of a function.
Why? Stacks can be as big as the implementation likes. Caution suggests we
don''t overdo it, since we know some popular implementations are a little ...
weedy ... but (unless the Standard says otherwise) that''s a property of
implementations, not of the language.
Because once I''ve seen someone declare a 1 megabyte char array in global
space like this:

char s[1000000];

and it worked.



Machine must have had enough room for it, then.

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html


Shuo Xiang wrote:


Greetings:

I know that variables declared on a stack definitely does not reside in heap
space so there is only a very limited amount of stuff that you can store in
the stack of a function. But what about the global space? Is that the same
as the heap space or is that still a form of special stack? Because once
I''ve seen someone declare a 1 megabyte char array in global space like this:

char s[1000000];

and it worked.



First, the C answer. C has no "stack," no "heap," and no
"global space." C supports data objects with three kinds of
"storage duration," namely, automatic, dynamic, and static.
The details of where these objects are created and how their
lifetimes are managed is entirely up to the implementation; the
C Standard specifies how the objects behave, but says nothing
about how the behavior is achieved. The implementation may (or
may not) impose different size limitations on objects of the
three different storage durations; again, the C Standard is
silent on what form such strictures might take.

Second, the practical answer. Many C implementations use
a stack for automatic-duration variables, and many implementations
impose stricter size limits for stack-resident objects than for
other kinds of objects. The details of these limits (including
whether and how they can be adjusted) are implementation-specific,
and can only be answered by consulting the implementation''s
documentation and/or gurus.

Finally, note that a C implementation is not required to be
able to support a one-megabyte array at all, regardless of what
storage duration is used. Many implementations do in fact handle
objects of this size and larger, but the Standard does not require
"minimalist" C to be able to do so.

--
Er*********@sun.com



"Chris Dollin" <ke**@hpl.hp.com> wrote in message


Why? Stacks can be as big as the implementation likes. Caution suggests
we don''t overdo it, since we know some popular implementations are a
little ... weedy ... but (unless the Standard says otherwise) that''s a property of implementations, not of the language.


There are however good reasons for providing small stacks. Firstly stack
usage increases only as the logarithm of the size of a structured program.
Secondly, most processors implement cache schemes and if the stack is small
enough to ensure that it is always held in the cache, there is likely to be
a performance improvement. Thirdly, it is unusual to know the size of a big
data item at compile time, and pre 99 C doesn''t allow variable size stack
arrays.


这篇关于堆栈空间,全球空间,堆空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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