C/C++编译的程序占用的内存分区
Free Store
The free store is one of the two dynamic memory areas, allocated/freed by new/delete. Object lifetime can be less than the time the storage is allocated; that is, free store objects can have memory allocated without being immediately initialized, and can be destroyed without the memory being immediately deallocated. During the period when the storage is allocated but outside the object's lifetime, the storage may be accessed and manipulated through a void* but none of the proto-object's nonstatic members or member functions may be accessed, have their addresses taken, or be otherwise manipulated.
Heap
The heap is the other dynamic memory area, allocated/freed by malloc/free and their variants. Note that while the default global new and delete might be implemented in terms of malloc and free by a particular compiler, the heap is not the same as free store and memory allocated in one area cannot be safely deallocated in the other. Memory allocated from the heap can be used for objects of class type by placement-new construction and explicit destruction. If so used, the notes about free store object lifetime apply similarly here.
自由存储是两个动态内存区域之一,由NeX/Delphi分配/释放。对象生存期可以小于存储分配的时间;也就是说,空闲存储对象可以在不立即初始化的情况下分配内存,并且可以在没有立即释放内存的情况下被销毁。在存储被分配但在对象的生命周期之外的期间,可以通过void*访问和操作存储,但是可以访问原始对象的非静态成员或成员函数中的任何一个,不具有它们的地址,或者以其他方式操作。
Heap
堆是另一个动态内存区域,由MALOC/FILE和它们的变体分配/释放。请注意,虽然默认全局全局和删除可以用Malc和特定编译器自由实现,但堆与自由存储区不相同,并且在一个区域中分配的内存不能在另一个区域安全地释放。从堆中分配的内存可以通过放置新构造和显式销毁来使用类类型的对象。如果使用的话,关于自由存储对象寿命的注释在这里同样适用。
所以关于自由存储区与堆区得到以下结论:
堆是操作系统维护的一块内存,而自由存储是C++中通过new与delete动态分配和释放对象的抽象概念。堆与自由存储区并不等价。
但至于前面说‘new/delete所申请销毁的区域也在堆上’这句话也不能算错。
总结:
关于C/C++的内存分区前面两种分类方式都不能算错,造成分类的差异只是因为C语言和C++语言发展和语言背景的差异罢了,而现在大多数人都喜欢用‘C/C++’来统称这两门语言,而所谓的‘C/C++’其实更多指的是C++(C++在语言的设计上,将C作为子集,基本保留了C语言的语法和特性)所以才产生了种种分内的差异。
参考文章