When is memory allocated during compilation?

后端 未结 8 1904
说谎
说谎 2020-12-18 15:18

When I write

int main()
{
    int j;
}

The memory for j is allocated at the time of compilation, but when during compilation?

8条回答
  •  我在风中等你
    2020-12-18 15:21

    The compilation process doesn't allocate the memory. It generates the code that allocates the memory :)

    In this case j would be a so-called stack variable and it would be allocated when execution enters the main() function. Global and static variables are allocated on the heap instead.

    Here's a short explanation: http://www.costech.or.tz/cs231/websites/C%20Programming/www-ee.eng.hawaii.edu/Courses/ee150/Book/chap14/subsection2.1.1.8.html. I'll see if I can find a better one.

提交回复
热议问题