When is memory allocated during compilation?

后端 未结 8 1894
说谎
说谎 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:30

    I think you are looking at the stages of compilation, rather than the memory allocation of 'j'. Since I think so, here is what happens:

    Once you feed your source code to the C compiler, the first stage(s) is(are) lexical and semantic analysis in which syntax and the semantics of the source code are analysed for correctness. If an error(s) was found, the compiler reports accordingly and does not go ahead. If no errors were found, it proceeds to the generation of a intermediate representation of the source code, usually after various optimisations. This intermediate representation can be in a native language (native to the OS/architecture, like in C) or a platform independent bytecode (like Python/Java..). The function of the compiler ends here.

    Memory allocation happens only when you execute the code. This is the runtime of the program. This comes only after the compilation stage, which probably you wouldn't want to know here. If you want to, please let me know. I shall try and add whatever I know.

    HTH.

提交回复
热议问题