Where are global variables located in the activation record for C?

瘦欲@ 提交于 2020-02-06 08:05:49

问题


In C, each function has an activation record which is allocated on a stack frame. Local variables are allocated in their own function's activation record. So, what is the case with global variables? Where are they allocated?

For example

#include <stdio.h>
    int a;

    void v()
    {a= 2;
    int b;
    b++;
    }

    main()
    {
    int f;
    printf("\n%d",a);
    v();
    }


-----Activation record----

-------------------
-------------------
activation record for main
-------------------
int f
-------------------
-------------------
activation record of v
--------------------
int a
--------------------
int b
--------------------
---------------

Where is variable x stored according to the activation record logic?


回答1:


In C each function has an activation record which is allocated on a stack frame.

Nope. However, this is how it is usually solved by the compiler. At least if you have not activated any optimizations.

Firstly, the C standard does not say anything about a stack at all. So an answer to this will be about how it's usually solved in practice.

And usually they are in the data segment or bss segment. A typical layout looks like this:

Stack - Grows down towards the heap. Used for local variables.
-----
...
...
...
----
Heap - Grows up towards the stack. Used for dynamically allocated memory.
----
BSS - Uninitialized data. Used for uninitialized global and static variables.
----
Data - Initialized data. 
----
Text - Runnable code



回答2:


In C each function has an activation record which is allocated on a stack frame.

Wrong, an optimizing compiler might not do that (and gcc -O3 -flto won't, on Linux / x86-64 with a recent GCC). It will inline some functions. Some locals are only kept in some processor registers (so have no memory location). Read about register allocation, e.g. in the Dragon Book or some other textbook about compilers. Be aware of automatic variables. Be also aware that you don't even need a computer to run a C program (a good way of teaching C is to have the classroom play being a computer; and you could run a C program on paper with a pencil).

The globals are usually not in practice on the call stack (which hold call frames or activation records). They might sit in the data segment (and could be optimized out entirely).

The C11 specification does not require any call stack. Check by reading n1570. Some implementations don't use any call stack (or activation records). Be aware of the crt0 calling your main.

Read linkers and loaders for more. Read also a textbook about operating systems.

On Linux, try cat /proc/self/maps to understand the virtual address space of the process running that cat command; see proc(5)

Look into the assembler code generated by gcc -O2 -fverbose-asm -S, using Linux. Read about invoking GCC.

See also this answer.

On Linux, play with nm(1), readelf(1), objdump(1) on your executable or object file (in ELF format).



来源:https://stackoverflow.com/questions/58791133/where-are-global-variables-located-in-the-activation-record-for-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!