簡體   English   中英

全局變量如$ _GLOBAL,$ _POST等存儲在哪里?

[英]Where is global variables like $_GLOBAL , $_POST etc stored?

當我參加面試時,面試官問我這個問題。 他們使用堆,堆棧等內存。我用Google搜索但我沒有得到任何明確的答案。

好吧,既然你標記了C ,我就會從那開始。

在C運行時,全局變量存儲在兩個位置之一; 數據段或BSS段。 確定特定變量屬於哪一個的方式是它是否已初始化。

global (and static) variables go inside the . 全局(和靜態)變量進入

global (and static) variables go inside the . 全局(和靜態)變量進入

在視覺上,整個運行時看起來像這樣:

 _______
|  Text |
|_______|
|  Data |   <-- Initialized globals / statics
|_______|
|  BSS  |   <-- Uninitialized globals / statics (basically a bunch of 0s)
|_______|
|       |
| Stack |
|_______|
|       |
|  Heap |
|_______|

and the , which are created at runtime, global variables exist as part of your program's executable image file ( a.out , foobar.exe ). 與在運行時創建的變量不同,全局變量作為程序的可執行映像文件( a.outfoobar.exe )的一部分存在。

內部$_POST的值在php_auto_globals_create_post()中創建,並通過PG(http_globals)[TRACK_VARS_POST] ,這只是一種引用http_globals

上述http_globals的定義告訴我們它是一個zval *元素數組,每個$_POST$_GET$_COOKIE等一個(數組也存儲在zval容器中)。

分配zval是通過ALLOC_ZVAL() ,它調用以下函數:

  1. _emalloc()
  2. _malloc()

malloc()函數在堆上分配內存,因此答案是

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM