数组声明:全球VS本地 [英] Array declaration : Global Vs Local

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

问题描述

为什么我们不能宣布1000万整数数组(或者说,足够大)的主要功能的函数内部或内部局部,而有可能在全球范围?

Why can't we declare an array of 10000000 integers (or say, large enough) inside the main function or inside any function locally while it is possible globally?

推荐答案

TL;博士:作为局部变量的空间是有限的。这是怎样的CPU和操作系统的工作。

tl;dr: space for local variables is limited. That's how CPU's and Operating Systems work.

这是受很多语言和实现,如共享的实现细节C,C ++典型的桌面操作系统。

This is an implementation detail that is shared by many languages and implementations, e.g. C, C++ on typical desktop OS.

其中code运行在大多数平台上预留一些内存称为的的Stack 的用于回报不会忽略和本地存储。这其中的CPU(英特尔,AMD等)如何执行机器code只是一个细节。

Most platforms where the code runs on set aside some memory called the Stack to be used for return adresses and local storage. This is just a detail of how the CPU (Intel, AMD, etc.) execute machine code.

从堆栈中分配是非常快的,但是内存才有效,直到函数调用返回。这使得它非常适用于C / C ++的局部变量。

Allocation from the stack is very fast, but the memory is valid only until the function call returns. This makes it ideal for C/C++ local variables.

不过,堆栈空间有限,所以大的分配将失败,
堆栈溢出的 - 即使仍然有足够的内存别处。

However, stack space is limited, so a large allocation will fail with a Stack Overflow - even if there's still enough memory "elsewhere".

在程序启动时对全局变量分配内存。例如。可执行文件将表明我需要用零填充这么大的空间,我需要为code数据这么大的空间,而且这么大的空间。

Memory for global variables is allocated when the program starts. E.g. the executable would indicate "I need so much space filled with zeroes, and I need so much space for data, and so much space for code."

第三存储位置为的的:它是用于与的malloc / 新分配的内存<堆/ code>等分配比堆栈更昂贵的(并且有更多的问题,以应付),直到你释放他们,这是有好有负担,他们呆在身边。

The "third" memory location is the Heap: it is used for memory allocated with malloc / new etc. Allocation on the heap are more expensive than on the stack (and have more problems to cope with), and they stay around until you free them, which is both good and a burden.

一些旁注,故意留下了,因为它不是一个问题直接相关的:

Some side notes, intentionally left out because it's not directly relevant for the question:

堆栈只是一个(contiguos)内存,在那里你可以只从顶部分配和释放内存的范围。这使得有限的,方便,快捷。

The Stack is just a range of (contiguos) memory, where you can allocate and free memory only from the top. This makes it limited, convenient and fast.

在现代桌面系统中,32位进程通常不会出现内存不足的跑了,但出来的地址空间:还是有可用的物理内存,而32位字的所有可用可能不会忽略被用完。

On modern desktop systems, 32 bit processes usually don't run out of memory anymore, but out of address space: there's still physical memory available, but all available possible adresses in a 32 bit word are used up.

执行的每个线程都有自己的堆栈,而全局变量和堆是一个进程的所有线程之间共享。

Each thread of execution gets its own stack, whereas global variables and the heap are shared between all threads of a process.

为什么不编译器移到别处大的分配?

Why don't compilers move large allocations elsewhere?

首先,它一直是这样。很多现有的code可巧妙地依赖于旧的行为,而精益求精的编译器可能会打破这个code。

First, "it has always been this way". A lot of existing code may subtly depend on the old behavior, and "improving" the compiler might break this code.

其次,由于种种原因,只适合一般别处是堆。栈和堆分配之间的性能差异是显著:

Second, for various reasons, the only generally suitable "elsewhere" is the heap. The performance difference between stack and heap allocations is significant:


  • 堆分配是比较昂贵

  • 堆共享,所以访问堆从多个线程必须同步它可以是非常昂贵的。

  • 堆的顶部几乎总是在CPU的缓存中(因为这ADRESS范围非常频繁地访问)。堆很可能不是

大多数时候,这些细节并不重要,但对于某些操作,这种差异是显著。如果编译器将决定把一些分配在堆上,我们将失去predictability。

Most of the time, these details don't matter, but for some operations, this difference is significant. If the compiler would decide to put some allocations on the heap, we would lose predictability.

这篇关于数组声明:全球VS本地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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