When is memory allocated during compilation?

后端 未结 8 1864
说谎
说谎 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.

    0 讨论(0)
  • 2020-12-18 15:22

    I guess you are mixing things up.

    Compiler doesn't allocate memory for variables - it generates code that allocates memory for variables at runtime. For globals is will be added to program start-up code.

    0 讨论(0)
  • 2020-12-18 15:23

    It is up to the compiler where to place j. Usually local variables are placed on the stack and as such for your particular example, the compiler will probably reserve the space on the stack for the duration of the main function. Note that this is different from global variable memory, which may receive its own memory.

    0 讨论(0)
  • Not at the time of compilation, your 'int j' will get allocated at application startup, when the application enter main() scope (actually it will not technically get allocated, as the stack is being used), globals will get allocated at runtime before entering main() scope.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-18 15:32

    In C, main is compiled the same as every other function: any variables declared in main will be "allocated" on the stack. A stack frame is the portion of the stack that is used by a single function call. The frame contains slots for all of the locals used within a function. This memory is considered temporary since when the function returns, this frame will be popped off the stack.

    The C compiler will assign a static address to global variables. This address is considered part of the binary's "image" and as such has a static location in memory. The C compiler knows the size of every type, so it can set aside the appropriate amount of space in the memory layout of the binary for each global variable. Then, any code that accesses this variable will simply reference this address instead.

    You can examine a variable's address with code like this:

    #include<stdio.h>
    
    int i;
    
    void foo(int n)
    {
        if(n > 2)
            return;
    
        printf("From foo &n = %xd\n", &n);
        printf("From foo &i = %xd\n", &i);
    
        foo(n+1);
    }
    
    
    int main()
    {
        printf("&i = %xd\n", &i);
        foo(0);
        return 0;
    }
    

    Running this code produces output similar to:

    ./a.out 
    &i = 600934d
    From foo &n = 38bc4efcd
    From foo &i = 600934d
    From foo &n = 38bc4eccd
    From foo &i = 600934d
    From foo &n = 38bc4e9cd
    From foo &i = 600934d
    

    There are two things you should notice here:

    1. The address of i is constant every time it is referenced
    2. The address of n (a variable local to the function foo changes with each call to foo. In fact, it will decrease every time, since the stack grows downward.
    0 讨论(0)
提交回复
热议问题