What's the difference between stack and heap?
来源:互联网 发布:linux查看挂载的磁盘 编辑:程序博客网 时间:2024/05/17 01:41
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.
The Stack is a Hardware Supported Feature in terms of Microprocessor Registers and Segments.
The Heap is actually a Software Abstraction.
For more information on the Stack, read this thread.
Also, on a side note - when one allocates using "new" one allocates on the Free Store.
Often, the Free Store is implemented as the "heap".
For more information on Free Store, Stack and Heap, please read this article.
Here is a sample code:
Stack is where variables declared before runtime are stored.
Heap is where variables created or initialized at runtime are stored.
- What's the difference between stack and heap?
- What's the difference between Heap and Stack
- What’s the difference between a stack and a heap?
- CareerCup What is the difference between a computers heap and it's stack?
- The difference between stack and heap
- Stack vs. Heap: What's the difference?
- difference between stack and heap
- What's the difference between an event and a delegate?
- what's the difference between OnDraw and OnPaint
- What's the difference between LastWriteTime and ChangeTime in FILE_BASIC_INFO?
- What's the difference between overload and override
- What's the difference between glibc and libc6?
- What's the difference between "but" and "yet"
- What's+the+difference+between+virtualization+and+cloud+computing?
- What's the difference between parallel and concurrent?
- What's the difference between program arguments and VM arguments?
- What's the difference between Template and Table in Smartforms
- What's the difference between “local” and “my” in Perl
- jsp中读取properties文件
- C#取硬盘序列号
- numdiff使用
- Flex开发者需要知道的10件事
- 乱弹火影世界,影射现实生活
- What's the difference between stack and heap?
- Java同步机制--synchronized
- 2月22日
- SWT中使用AWT组件
- 完全解开magento单产品多图片批量上传之谜
- magento 傻瓜式迁移
- Lonely Years (v1.0)
- 取当前用户登录域和登录名
- Linux crash dump文件生成与使用