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

后端 未结 2 2038
终归单人心
终归单人心 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:08

    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).

提交回复
热议问题