heap stack区别简介

转载 2012年03月28日 00:05:27
You can imagine the stack as being like a stack of plates. As soon as a function calls some other function things get added to the stack (from the bottom, upwards) starting with the return address (i.e. where the program flow should go back to, once the called function has finished).

Stack memory
Let's suppose there was nothing on the stack to begin with. One of the functions (either the calling function or the called function) would start a stack by placing a return address - and some extra 'plates' might be added, representing any arguments that get passed to the called function (the called function's parameters).

Program execution then gets passed to the called function which probably adds some more plates in the form of 'local variables'. Every 'plate' has some sort of information written on it that means something special to te program. By the time the called function reaches its return statement it has a big stack of plates. Just before it returns, it removes the plates (one by one) from the top downward so that it can read the return address, written on that very bottom plate. The program then jumps back to that original return address.

The most important thing about the stack is that you don't need to think much about it. Your program manages it. Also, stack management is very efficient because the plates don't literally need to be removed (like they would if they were real plates). In reality, the program contains a "stack pointer" that simply gets updated each time a particular plate needs to be written to or read from. The size of the stack is usually quite small because it's mostly used for local variables (variables that go out of scope pretty soon after they're finished with). The size of the stack is set at compile time and is small enough to be available on any computer, no matter how little RAM it's got.

Heap memory
The heap is a different thing altogether. Different PC's have different amounts of memory so it would be silly to have to allocate all the memory at compile time. If a particular computer has a lot of RAM, why not make use of it? Therefore the heap consists of memory that can be allocated at run time. The amount of heap memory is different for different machines and a well written program will take account of this (allocating heap memory sparingly - and not assuming that everyone will have massive amounts of it). Heap memory is most often used for 'large' things (e.g. the contents of a file) - or for things that need to 'stay around' in memory so that different functions can share the data (in other words, for NON-local variables). It's also used very frequently to allocate memory for things whose size is not known until run time. For example, if you had to open a file and read in the data, you don't know how much data exists until run time. In contrast, the size of objects allocated on the stack, needs to be known at compile time.

Whereas your program takes care of stack memory (allocating it & cleaning it up) it's the programmer's job to deal with heap memory. The size of objects allocated on the heap is often not known to the compiler - therefore the programmer must allocate and release the memory specifically.


  • u014306011
  • u014306011
  • 2016年04月02日 15:30
  • 3662


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


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

操作系统中heap 和 stack的区别

  • program_guys
  • program_guys
  • 2017年11月18日 12:43
  • 1204

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

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

java中 stack与和heap的区别

java的内存分为栈(stack)和堆(heap); 1、栈(stack)  (1)定义         每个应用运行时都有自己的一段内存空间,用于存放临时变量、参数传递、函数调用时的PC值以及...
  • u013485584
  • u013485584
  • 2015年11月18日 11:13
  • 332


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


学习编程的时候,经常会看到stack这个词,它的中文名字叫做"栈"。 理解这个概念,对于理解程序的运行至关重要。容易混淆的是,这个词其实有三种含义,适用于不同的场合,必须加以区分。 含义一...
  • smartboy_01
  • smartboy_01
  • 2014年04月04日 09:57
  • 3736


栈(stack) 由编译器自动分配释放管理。局部变量及每次函数调用时返回地址、以及调用者的环境信息(例如某些机器寄存器)都存放在栈中。新被调用的函数在栈上为其自动和临时变量分配存储空间。 堆(heap...
  • seanchen88
  • seanchen88
  • 2016年11月29日 08:57
  • 446

转: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
  • 697
您举报文章:heap stack区别简介