What and where are the stack and heap?

翻译 2015年11月17日 20:45:38

The stack is the memory set aside as scratch space for a thread of execution. When a function is called, a block is reserved on the top of the stack for local variables and some bookkeeping data. When that function returns, the block becomes unused and can be used the next time a function is called. The stack is always reserved in a LIFO (last in first out) order; the most recently reserved block is always the next block to be freed. This makes it really simple to keep track of the stack; freeing a block from the stack is nothing more than adjusting one pointer.

The heap is memory set aside for dynamic allocation. Unlike the stack, there's no enforced pattern to the allocation and deallocation of blocks from the heap; you can allocate a block at any time and free it at any time. This makes it much more complex to keep track of which parts of the heap are allocated or free at any given time; there are many custom heap allocators available to tune heap performance for different usage patterns.

Each thread gets a stack, while there's typically only one heap for the application (although it isn't uncommon to have multiple heaps for different types of allocation).

To answer your questions directly:

To what extent are they controlled by the OS or language runtime?

The OS allocates the stack for each system-level thread when the thread is created. Typically the OS is called by the language runtime to allocate the heap for the application.

What is their scope?

The stack is attached to a thread, so when the thread exits the stack is reclaimed. The heap is typically allocated at application startup by the runtime, and is reclaimed when the application (technically process) exits.

What determines the size of each of them?

The size of the stack is set when a thread is created. The size of the heap is set on application startup, but can grow as space is needed (the allocator requests more memory from the operating system).

What makes one faster?

The stack is faster because the access pattern makes it trivial to allocate and deallocate memory from it (a pointer/integer is simply incremented or decremented), while the heap has much more complex bookkeeping involved in an allocation or free. Also, each byte in the stack tends to be reused very frequently which means it tends to be mapped to the processor's cache, making it very fast. Another performance hit for the heap is that the heap, being mostly a global resource, typically has to be multi-threading safe, i.e. each allocation and deallocation needs to be - typically - synchronized with "all" other heap accesses in the program.

What and where are the stack and heap?

http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap What and...
  • qq469236803
  • qq469236803
  • 2013年09月09日 16:02
  • 1006


原文地址:http://blog.csdn.net/abcjennifer/article/details/39780819 堆:顺序随意  栈:先进后出  堆和栈的...
  • Meta_Cpp
  • Meta_Cpp
  • 2015年01月09日 14:02
  • 2803

Stack and Heap

JVM  分配内存进入以下区域 1: Heap 2:Stack 3:Code 4:Static jvm有效的管理分配到这几个内存区域。 Code  section  代码区 包含这个 字节...
  • topwqp
  • topwqp
  • 2013年01月27日 22:32
  • 2951

What is heap and stack?

http://www.maxi-pedia.com/what+is+heap+and+stack The stack is the section of memory that is allocate...
  • lijian818181
  • lijian818181
  • 2010年02月27日 22:23
  • 309

c++中堆和栈的的区别(stack vs heap)

 堆和栈的区别 一、预备知识—程序的内存分配 一个由c/C++编译的程序占用的内存分为以下几个部分 1、栈区(stack)— 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等。其操...
  • hyqsong
  • hyqsong
  • 2014年12月18日 15:57
  • 2903


stack:         具体地说,现代计算机(串行执行机制),都直接在代码底层支持栈的数据结构。这体现在,有专门的寄存器指向栈所在的地址,有专门的机器指令完成数据入栈出栈的操作。这种机制的特点...
  • ydonghao2
  • ydonghao2
  • 2013年10月25日 19:24
  • 2909


2.1 内存分配策略按照编译原理的观点,程序运行时的内存分配有三种策略,分别是静态的,栈式的,和堆式的.静态存储分配是指在编译时就能确定每个数据目标在运行时刻的存储空间需求,因而在编译时就可以给他们分...
  • zlghitler
  • zlghitler
  • 2007年03月07日 20:58
  • 7295

转:Heap memory和Stack memory的区别

转:http://my.opera.com/jijidao/blog/show.dml/3216696 摘录三段,一是c中的,二是对于java的,三是从os角度看的。 Stack vs H...
  • veryv
  • veryv
  • 2011年06月22日 03:22
  • 723

栈的理解 IAR/MDK修改stack/heap

  • huan447882949
  • huan447882949
  • 2017年12月13日 14:19
  • 169


根源出处已不可考证,不过写的很好,转来当作备忘。 一个程序一般分为3段:text段,data段,bss段。 TEXT 段:就是放程序代码的,编译时确定,只读;DATA段:存放在编译阶段(而非运行时...
  • yOung_One
  • yOung_One
  • 2014年12月02日 19:15
  • 1442
您举报文章:What and where are the stack and heap?