[C++]存儲區域及其功能特點

C++ 有幾個特定的區域用來存儲對象或非對象值,每個區域有其各自的特點。
https://stackoverflow.com/questions/14588767/where-in-memory-are-my-variables-stored-in-c
Global variables -------> data
Static variables -------> data
Constant data types -----> code
Local variables (declared and defined in functions) --------> stack
Variables declared and defined in main function -----> heap
Pointers (for example, char *arr, int *arr) -------> heap
Dynamically allocated space (using malloc and calloc) --------> stack
http://pierre.aubert.free.fr/divers/gotw/gotw009a.html

\[\begin{array} {c|c} \text{區域} & \text{功能特點} \\ \hline \\ \hline 常量數據 & 常量數據區存儲了字符和其他在編譯期間就已經知道的數據。類類型的對象不能存儲在這個區域。所有在這個區域的數據都在程序的整個生命週期有效。這裏的所有數據都是隻讀數據,也就是說修改數據這個行爲在這裏是未定義的,一部分原因是雞屎樹底層的存儲格式也是受制於實現的隨即優化的。比如說,對於某個特定的編譯器,數組可能被存儲在重疊的對象(overlapping objects)中,如果它想的話。 \\ \hline 棧& 棧區存儲了自動變量。通常比動態存儲(堆或者自由存儲區)要快,因爲存儲的分配(allocation)只涉及指針的增長而沒有更復雜的存儲管理。當存儲空間被分配後對象立即就被構造,而對象析構後存儲空間也能立即被釋放。因此,程序員沒有機會直接地操作已分配且未初始化的棧空間(禁止使用顯式dtor故意篡改和放置new (barring willful tampering using explicit dtors and placement new)) \\ \hline 自由存儲區 & new \\ \hline 常量存儲區 & 存儲常量數據 \\ \hline 靜態存儲區 & 存儲靜態成員變量 \\ \end{array}\]

在C++裏,通常new出來的對象時在空閒存儲區裏的,而malloc出來的空間則是在堆裏。
空閒存儲區的第一個特點時,其中分配的對象沒有名字。new表達式沒有返回實際分配的對象,二十返回指向該對象的指針。對該對象的全部操作都要通過這個指針間接完成。例如:

int* pi = new int;

該new表達式會創建一個int型對象,由pi指向它。
空閒存儲區的第二個特點時,分配的內存未初始化。空閒存儲區的內存包含隨機的位模式。它是程序運行前該內存上次被使用留下的結果。

Const Data The const data area stores string literals and
other data whose values are known at compile
time. No objects of class type can exist in
this area. All data in this area is available
during the entire lifetime of the program.
常量

             Further, all of this data is read-only, and the
             results of trying to modify it are undefined.
             This is in part because even the underlying
             storage format is subject to arbitrary
             optimization by the implementation.  For
             example, a particular compiler may store string
             literals in overlapping objects if it wants to.

Stack The stack stores automatic variables. Typically
allocation is much faster than for dynamic
storage (heap or free store) because a memory
allocation involves only pointer increment
rather than more complex management. Objects
are constructed immediately after memory is
allocated and destroyed immediately before
memory is deallocated, so there is no
opportunity for programmers to directly
manipulate allocated but uninitialized stack
space (barring willful tampering using explicit
dtors and placement new).

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.

Global/Static Global or static variables and objects have
their storage allocated at program startup, but
may not be initialized until after the program
has begun executing. For instance, a static
variable in a function is initialized only the
first time program execution passes through its
definition. The order of initialization of
global variables across translation units is not
defined, and special care is needed to manage
dependencies between global objects (including
class statics). As always, uninitialized proto-
objects' storage may be accessed and manipulated
through a void* but no nonstatic members or
member functions may be used or referenced
outside the object's actual lifetime.
Note about Heap vs. Free Store: We distinguish between "heap" and "free store" because the draft deliberately leaves unspecified the question of whether these two areas are related. For example, when memory is deallocated via operator delete, 18.4.1.1 states:

It is unspecified under what conditions part or all of such reclaimed storage is allocated by a subsequent call to operator new or any of calloc, malloc, or realloc, declared in .

Similarly, it is unspecified whether new/delete is implemented in terms of malloc/free or vice versa in a given implementation. Effectively, the two areas behave differently and are accessed differently -- so be sure to use them differently!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章