笔记内容浅薄,小白个人在网上看大佬博客总结,若有不妥之处请联系,请大佬指教,愿意学习。
对于栈,堆,静态区的学习和理解
栈区:在调用函数时,函数中的局部变量的存储单元是在栈上创建的,ha
函数执行结束后,这些存储单元被自动释放。
堆区:堆是动态分布内存,由程序员管理,若程序员不释放,结束后可能被os回收。
静态区:内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在。它主要存放静态数据、全局数据和常量。
下面是引自[Think in C++]的一段原文
The stack is an area in memory that is used directly by the microprocessor to store data during program execution.Variables on the stack are sometimes called automatic or scoped variables
The second approach is to create objects dynamically in a pool of memory called the heap.In this approach you don’t know until runtime how many objects you need, what their lifetime is, or what their exact type is. Those decisions are made at the spur of the moment while the program is running. If you need a new object, you simply make it on the heap when you need it, using the new keyword. When you’re finished with the storage, you must release it using the delete keyword.
The static storage area is simply a fixed patch of memory that is allocated before the program begins to run.
从这段话可以理解出对象在栈中(stack)和静态区(static)程序员不能使用new和Delete来对其操作.因此栈中(stack)和静态区(static)的变量是不受程序员的程序控制的,既然不是由程序控制的?那是由誰控制的?其实可以这样理解的:由编译器控制的.
而堆(heap),C++的程序员则可以通过new 和delete的指令对其进行操作,因此給程序编写带来了灵活性.当然对于Java或者是C#的程序员来说,堆(heap)的控制也由不得他了,而是Java或C#内部的垃圾回收器帮程序员来打理,通常Java或者是C#的程序员只要new下就不用去delete了,这个delelte则是由内部的垃圾回收器去打理了