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

老子叫甜甜 提交于 2019-12-19 03:15:11

问题


When i attended an interview, the interviewer asked me this question. Which memory they are using heap , stack etc. I googled it but i didn't get any clear answer.


回答1:


The values of $_POST internally are created inside php_auto_globals_create_post() and made available via PG(http_globals)[TRACK_VARS_POST], which is just a way to reference http_globals.

The definition of aforementioned http_globals tells us that it's an array of zval * elements, one for each $_POST, $_GET, $_COOKIE, etc. (arrays are also stored inside a zval container).

Allocating a zval is done via ALLOC_ZVAL(), which calls the following functions:

  1. _emalloc()
  2. _malloc()

The malloc() function allocates memory on the heap, so therefore the answer is heap.




回答2:


Well, since you tagged C, I'll start with that.

In the C runtime, global variables are stored in one of two places; the data segment or the BSS segment. The way you determine which one a particular variable belongs to is whether or not it is initialized.

Initialized global (and static) variables go inside the data segment.

Uninitialized global (and static) variables go inside the BSS segment.

Visually, the entire runtime looks like this:

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

Unlike variables on the stack and the heap, which are created at runtime, global variables exist as part of your program's executable image file (a.out, foobar.exe).



来源:https://stackoverflow.com/questions/19488318/where-is-global-variables-like-global-post-etc-stored

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!