C的激活记录中的全局变量在哪里? [英] Where are global variables located in the activation record for C?

查看:122
本文介绍了C的激活记录中的全局变量在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C中,每个功能都有一个激活记录,该记录分配在堆栈帧上.局部变量分配在它们自己的函数的激活记录中.那么,全局变量是什么情况?他们在哪里分配?

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?

例如

#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
--------------------
---------------


根据激活记录逻辑,变量x存储在哪里?


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

推荐答案

在C中,每个函数都有一个激活记录,该记录分配在堆栈帧上.

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.

首先,C标准根本没有说明任何有关堆栈的内容.因此,对此的答案将是在实践中通常如何解决该问题.

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.

通常它们在数据段或bss段中.典型的布局如下所示:

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

这篇关于C的激活记录中的全局变量在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆