堆和栈的记忆是如何mananged,实施分配 [英] How is heap and stack memories mananged, implemented, allocated

查看:199
本文介绍了堆和栈的记忆是如何mananged,实施分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能显示的文件:

  <一href=\"http://stackoverflow.com/questions/1212797/how-is-heap-and-stack-memories-mananged-implemented-allocated\">How被堆和栈的回忆mananged,实施分配?

  堆栈,静态和堆在C ++

在C / C ++,我们可以存储无论是在栈或堆变量,函数,成员函数实例的一类。

In C/C++ we can store variables, functions, member functions, instances of a class either on a stack or a heap.

是如何实现的每一个?它是如何管理(高电平)?请问GCC preallocates一个内存块用于栈和堆,然后多尔斯了要求?是原装内存RAM中来的?

How is each implemented? How is it managed (high level)? Does gcc preallocates a chunk of memory to be used for the stack and heap, and then doles out on request? Is original memory coming from RAM?

能否函数在堆中,而不是股票分配?

Can a function be allocated on the heap instead of a stock?

             --Clarification--

我真的问执行和堆的管理和栈的回忆。 阅读引用的问题,我没有找到任何解决了之后.. 。感谢您的链接

I am really asking about implementation and management of heap and stack memories. After reading referenced question, I didn't find anything that addresses that... thanks for the link

推荐答案

我觉得你的问题可以很容易地写出至少在书中对操作系统的一些章节。我建议你​​阅读的Tanenbaum:现代操作系统

I think to your question one can easily write at least some chapters for the book on Operating Systems. I suggest you to read Tanenbaum: Modern Operating Systems.

堆和栈的主要差别,一个是每个进程的项目,其他的每个线程的项目。当程序被启动最初它得到一些最起码的堆和一些堆栈段。堆长大,堆栈(每个线程)静态的。如果你写不终止(无限递归),你会得到堆栈溢出的递归函数;)任何函数调用对堆栈段堆栈帧,当函数离开,堆栈展开和帧可以自由的使用下一个功能。栈是一个连续的线性结构。在Linux上,您可以通过一个环境变量配置进程堆栈段的大小。在窗口(至少在微软的Visual C ++)你可以通过一个链接器标志和堆栈段的大小。栈溢出,也可以在编译时分配一些大的阵列时产生的:

Main difference of heap and stack, that one is per process item, the other per thread item. Initially when program is started it gets some minimal heap and some stack segment. Heap is grown, stack is static (for each thread). If you write a recursive function which does not terminate (endless recursion) you will get stack overflow ;) Any function call has a stack frame on stack segment, when the function leaves, the stack is unwound and frame is free to be used by the next function. Stack is a continuous linear structure. On Linux you can configure the stack segment size for a process via an environment variable. On windows (at least with MS Visual C++) you can pass a linker flag with the size of stack segment. Stack overflows can also be produced when allocating at compile time some big array:

char test[1000000];

堆是一个不同的故事。当一个进程启动堆大小为一些默认值,可能会有所不同形式的操作系统OS或配置该操作系统上使用(例如,在Windows上它是默认情况2MB,据我记得)。此外,如果你需要更多的堆,分配变量等,它会增加更多的空间。如果程序不释放堆内存耗尽它(或堆空间)。有不同的数据结构堆实现他们中的一些二叉树衍生品,一些不是如斐波那契堆(树阿甘)。你可以阅读关于如何编写一个内存分配器一些文章等。这些数据结构必须寻找堆节点时分配的块需要被解除分配,或追加(找到一个空闲块)需要新的堆空间时进行优化。

Heap is a different story. When a process starts up heap size is some default value and can vary form OS to OS or configuration being used on that OS (e.g. on Windows it is 2MB by default, as far as I remember it). Further, if you need more heap, to allocate more space for variables etc. it will grow. If the program doesn't free heap memory it runs out of it (or heap space). There are different data structures for heap implementation some of them are binary tree derivatives, some are not e.g. Fibonacci Heap (forrest of tree). You can read some article etc. on how to write a memory allocator. These data structures must be optimized for finding the heap node when an allocated chunk needs to be de-allocated, or appending (finding a free chunk) when new heap space is needed.

在32位操作系统每个进程都有虚拟地址空间4GB。你可以想像不可能有那么多的RAM在那里与他们的虚拟地址空间契合4GBs所有进程。操作系统内存中的页面,其中被交换到高清不再需要或过期时举办。这是寻呼来玩。一切被映射到的网页:与堆栈或生长堆的方法。由于它动态增长堆的结构,它可以被放置在多个页面。这就是为什么堆访问可以是非常昂贵的,因为如果页面不在存储器中的页面发生故障和OS具有从磁盘加载页(并且,可以是由数量级慢)。正在执行的线程的栈帧是在处理器的高速缓存,这是更快的RAM中。

Each process on a 32 bit OS has 4GB of virtual address space. As you can imagine there can't be so much RAM where all processes with their 4GBs of virtual address space fit. OS memory is organized in pages, which are swapped to HD when no longer needed or expired. This is where paging comes to play. Everything is mapped to pages: a process with the stack or the growing heap. Due to the structure of heap that it grows dynamically, it can be placed on multiple pages. This is why heap access can be very expensive, because if the page is not in memory a page fault happens and OS has to load a page from the disk (and that can be by magnitude slower). Stack frame of the thread being executed is in processor cache, which is much faster as RAM.

不同堆的类型是可能的,有可能是堆这是非常快的量是在多线程环境中非常有效的小物体或堆。描述Alexandrescu的现代C ++设计怎样发展小对象分配器和管理小物件堆。这种实现是在他的洛基C ++库可用。一些嵌入式系统提供物理上不同的存储区域,其中不同堆的类型可以被实现ontop。要编写自己的分配器(堆管理器等),如果你想打一个编译器是一个艰苦的工作。

Different heap types are possible, there might be heaps which are very fast for small objects or heaps which are very efficient in multi-threaded environments. Alexandrescu describes in "Modern C++ Design" how to develop small object allocator and a heap which manages small objects. This implementation is available in his Loki C++ library. Some embedded systems offer physically different memory regions, where different heap types can be implemented ontop. To write an own allocator (heap manager etc.) is a hard job if you want to beat a compiler.

问候,

Ovanes

Regards,
Ovanes

这篇关于堆和栈的记忆是如何mananged,实施分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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