库拉索芦荟 - 芦荟汇聚地!

heap

什么是堆栈

临时寄存货物的地方。在计算机领域,堆栈是一个不容忽视的概念,堆栈是一种数据结构。堆栈都是一种数据项按序排列的数据结构,只能在一端(称为栈顶(top))对数据项进行插入和删除。在单片机应用中,堆栈是个特殊的存储区,主要功能是暂时存放数据和地址,通常用来保护断点和现场。堆栈是一个特定的存储区或寄存器,它的一端是固定的,另一端是浮动的。对这个存储区存入的数据,是一种特殊的数据结构。所有的数据存入或取出,只能在浮动的一端(称栈顶)进行,严格按照“后进先出”的原则存取,位于其中间的元素,必须在其栈上部(后进栈者)诸元素逐个移出后才能取出。

堆栈是什么意思

类似于队列,堆栈是个简单的数据存储结构。堆栈中数据进出的顺序很重要,举个例子,餐厅的盘子堆,盘子洗完要堆到上面,而不是插到下面的某个位置(相信不会有人那么做)。当厨师要用到盘子时从最上面的开始拿。即最先放在堆里的盘子会被最后一个用到。定义:堆栈就是只能在一端插入和删除数据的链表,这个端就叫做栈顶(top),最后一个添加的数据第一个被删除。因此,这也叫后进先出(LAST IN FIRST OUT)链表或是先进后出链表(FIRST IN LAST OUT)。对于堆栈有两种操作:进栈指令(PUSH):在栈中现有元素顶部添加一个元素,新加入的元素变为最顶端的元素。出栈指令(POP):取出栈顶元素,删除栈中的这个元素。有些情况下,栈的最大长度有限。如果栈中元素已经达到最大长度,再用进栈指令会造成堆栈上溢出(stack overflow),相似的,如果堆栈已空还用出栈指令会造成堆栈下溢出(stack underflow)。

Stack 和 Heap的区别

What is the stack? It's a special region of your computer's memory that stores temporary variables created by each function (including the main() function). The stack is a "LIFO" (last in, first out) data structure, that is managed and optimized by the CPU quite closely. Every time a function declares a new variable, it is "pushed" onto the stack. Then every time a function exits, all of the variables pushed onto the stack by that function, are freed (that is to say, they are deleted). Once a stack variable is freed, that region of memory becomes available for other stack variables. The advantage of using the stack to store variables, is that memory is managed for you. You don't have to allocate memory by hand, or free it once you don't need it any more. What's more, because the CPU organizes stack memory so efficiently, reading from and writing to stack variables is very fast. Another feature of the stack to keep in mind, is that there is a limit (varies with OS) on the size of variables that can be stored on the stack. This is not the case for variables allocated on the heap . https://www.gribblelab.org/CBootCamp/7_Memory_Stack_vs_Heap.html The heap is a region of your computer's memory that is not managed automatically for you, and is not as tightly managed by the CPU. It is a more free-floating region of memory (and is larger). To allocate memory on the heap, you must use malloc() or calloc() , which are built-in C functions. Once you have allocated memory on the heap, you are responsible for using free() to deallocate that memory once you don't need it any more. If you fail to do this, your program will have what is known as a memory leak . That is, memory on the heap will still be set aside (and won't be available to other processes). As we will see in the debugging section, there is a tool called valgrind that can help you detect memory leaks. Unlike the stack, the heap does not have size restrictions on variable size (apart from the obvious physical limitations of your computer). Heap memory is slightly slower to be read from and written to, because one has to use pointers to access memory on the heap. We will talk about pointers shortly. Unlike the stack, variables created on the heap are accessible by any function, anywhere in your program. Heap variables are essentially global in scope. When should you use the heap, and when should you use the stack? If you need to allocate a large block of memory (e.g. a large array, or a big struct), and you need to keep that variable around a long time (like a global), then you should allocate it on the heap. If you are dealing with realtively small variables that only need to persist as long as the function using them is alive, then you should use the stack, it's easier and faster. If you need variables like arrays and structs that can change size dynamically (e.g. arrays that can grow or shrink as needed) then you will likely need to allocate them on the heap, and use dynamic memory allocation functions like malloc() , calloc() , realloc() and free() to manage that memory "by hand". We will talk about dynamically allocated data structures after we talk about pointers. https://www.gribblelab.org/CBootCamp/7_Memory_Stack_vs_Heap.html http://net-informations.com/faq/net/stack-heap.htm 想要看到更多玮哥的学习笔记、考试复习资料、面试准备资料?想要看到IBM工作时期的技术积累和国外初创公司的经验总结? 敬请关注: 玮哥的博客 —— CSDN的传送门 玮哥的博客 —— 的传送门 玮哥的博客 —— 博客园的传送门

stack 和heap都是堆积的意思,两者有区别吗

在计算机语言中,stack 表示栈,heap表示堆,这是两个概念。
栈stack是计算机系统提供的具有后进先出特点的数据结构,
而堆heap是函数库提供的内部结构,为分配新内存空间服务的。

在日常英语中,二者都指堆积(动词)和一堆(名词),但是
heap 通常指杂乱的、呈小山状的一堆东西,如:Now, the house is a heap of rubble(现在,房子成了一堆瓦砾)。
stack通常是整齐的一叠,指扁平物体叠放起来,如:a neat stack of dishes(整齐的一叠盘子)。


什么是堆栈

堆是堆(heap),栈是栈(stack),虽然堆栈(heap and stack)有相似之处,但不要混为一谈。
本质上讲,堆(heap)是一种数据结构,是纯软件的实现。堆基于一定的程序基础(例如在操作系统),它更加偏向于软件实现动态的内存管理,令程序运行时根据所需来动态申请/释放内存。
而栈(stack)既存在软件实现又存在硬件实现。栈本质上是一种简单的先进先出结构,主要目的是为程序运行时保存关键的现场数据,尤其适合于(嵌套式)中断的配合。几乎所有的微控制器/微处理器都具备硬件栈。而软件/操作系统中又可以进一步建立软件栈,为线程建立专用的存储区域。


大家都在看