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

后端 未结 2 2031
终归单人心
终归单人心 2021-01-05 13:55

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.

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-05 14:09

    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.

提交回复
热议问题