用C局部变量和静态变量 [英] Local and static variables in C

查看:199
本文介绍了用C局部变量和静态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此编译:

  //外部定义
INT值1 = 0;
静态INT值2 = 0;

gcc编译器生成以下组件:

  .globl值1
        .bss段
        .align伪4
        .TYPE值1,@object
        .size值1,4
值1:
        .zero 4
        。本地VALUE2
        .comm value2,4,4

然而,当我初始化变量以外的值为零,如:

  //外部定义
INT值1 = 1;
静态INT值2 = 1;

gcc编译器生成如下:

  .globl值1
        。数据
        .align伪4
        .TYPE值1,@object
        .size值1,4
值1:
        1。长期
        .align伪4
        .TYPE值2,@object
        .size值2,4
VALUE2:
        1。长期

我的问题是:


  1. 为什么在第一种情况下的值在BSS段分配,而在数据段中的第二种情况。

  2. 为什么值2变量被定义为。本地和在第一种情况下.comm,而不是在所述第二


解决方案

一般来说, BSS 部分包含未初始化值和数据部分包含初始化值。但是,初始化为零到 BSS 部分,而不是数据部分海湾地区值,如 BSS 部分在运行系统中归零,无论如何,它并没有多大意义存放在数据部分,这样可以节省零一些磁盘空间,从人的gcc:


  

-fno-零初始化合BSS如果目标支持BSS段,的 GCC默认把被初始化为零到BSS变量。这个
  可以在生成的code节省空间。
此选项关闭此
  行为,因为某些程序明确依赖变量去
  数据部分


我不知道为什么 .comm 使用具有的静态的存储是本地的一个目标文件,它通常用来声明共同的符号,如果<青霉>不的定义/初始化,应当通过与具有来自其他对象文件相同的名称,这就是为什么因为变量被初始化它不是在第二个例子中使用的符号的接头合并,从 手册


  

.comm声明一个名为symbol的共用符号。当连接时,一个共同的
  在一个对象文件符号可以与限定的或共同被合并
  在另一目标文件的名称相同的符号


When compiling this:

// external definitions
int value1 = 0;
static int value2 = 0;

the gcc compiler generates the following assembly:

.globl value1
        .bss
        .align 4
        .type   value1, @object
        .size   value1, 4
value1:
        .zero   4
        .local  value2
        .comm   value2,4,4

However, when i initialize the variables to a value other than zero such as:

// external definitions
int value1 = 1;
static int value2 = 1;

the gcc compiler generated the following:

.globl value1
        .data
        .align 4
        .type   value1, @object
        .size   value1, 4
value1:
        .long   1
        .align 4
        .type   value2, @object
        .size   value2, 4
value2:
        .long   1

My questions are:

  1. Why in the first case the values are allocated in the bss segment while in the second case in the data segment.
  2. Why value2 variable is defined as .local and .comm in the first case, while not in the second.

解决方案

Generally speaking, the bss section contains uninitialized values and the data section contains initialized values. However, gcc places values that are initialized to zero into the bss section instead of the data section, as the bss section is zeroed out in runtime anyway, it doesn't make much sense to store zeros in the data section, this saves some disk space, from man gcc:

-fno-zero-initialized-in-bss If the target supports a BSS section, GCC by default puts variables that are initialized to zero into BSS. This can save space in the resulting code. This option turns off this behavior because some programs explicitly rely on variables going to the data section

I'm not sure why .comm is used with static storage which is local to an object file, it is usually used to declare common symbols that, if not defined/initialized, should be merged by the linker with symbol that have the same name from other object files and that's why it's not used in the second example because the variables are initialized, from the as manual

.comm declares a common symbol named symbol. When linking, a common symbol in one object file may be merged with a defined or common symbol of the same name in another object file

这篇关于用C局部变量和静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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