用C BSS段 [英] bss segment in C

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

问题描述

在回答这个问题的一个<一个href=\"http://stackoverflow.com/questions/1909953/regarding-the-bss-segment-and-data-segment-in-unix\">Regarding在Unix中 BSS段和数据段,我看到BSS的解释如下:

In one of the answers to the question "Regarding the bss segment and data segment in Unix", I see the explanation on bss as follows:

BSS是特殊的:.bss中的对象不采取目标文件中的任何空间,并通过分组没有具体intialized在一起的所有符号,它们可以轻松地一次归零

Bss is special: .bss objects don't take any space in the object file, and by grouping all the symbols that are not specifically intialized together, they can be easily zeroed out at once.

但是当我使用的目标文件的大小,出了code产生的:

But when I use size on the object file, generated out of the code:

#include <stdio.h>
int uninit_global_var;
int init_global_var=5;

int main()
{
   int local_var;
   return 0;
}

我有以下

text    data      bss    dec     hex filename
1231     280      12    1523     5f3 a.out

和看到基于与全球范围内的未初始化的数据成员BSS增长。因此,谁能证明提到的声明?

and see the bss growing based on the uninitialized data members with global scope. So can anyone justify the mentioned statement?

推荐答案

如果您删除文件stdio.h你的输出可能会更有意义。让我们忽视了图书馆,因为它包含的内部变量。

If you remove stdio.h your output will probably be more meaningful. Lets ignore that library, since it contains internal variables.

在特定的情况下,会发生以下情况:

In your specific case, the following happens:

int uninit_global_var;

由于这是在的分配的变量文件范围的中拥有的静态存储持续时间的,就像任何变量声明为静态。 C标准要求,如果具有静态存储持续时间的变量不是由程序员显式地初始化,如在这种情况下,它必须被设置为零,程序开始之前。所有这些变量都放在的.bss 段。

Since this is a variable allocated at file scope in has static storage duration, just as any variable declared as static. The C standard requires that if a variable with static storage duration is not initialized explicitly by the programmer, as in this case, it must be set to zero, before the program starts. All such variables are put in the .bss segment.

int init_global_var=5;

此变量也分配在文件范围内,所以它也将有静态存储持续时间。但在这种情况下,它是由程序员初始化。 C标准要求这些变量被设置为给定的值,该程序开始之前。这样的变量都放在。数据段。

This variable is also allocated at file scope, so it will also have static storage duration. But in this case it is initialised by the programmer. The C standard demands that such variables are set to the value given, before the program starts. Such variables are placed in the .data segment.

   int local_var;

该变量具有自动存储时间(本地)。编译器将最有可能优化掉这个变量,因为它填补了没有目的。但让我们假设这样的优化不会发生。该变量随后将在运行时进行分配,当范围(块)它驻留在被执行,然后停止一旦范围finsihed存在(它超出范围)。它将被无论是在堆栈或在CPU寄存器分配。换句话说,在连接时这个变量只存在作为节目code,在某些汇编指令说推堆栈上一个int,然后后来的从栈中弹出一个int的形式。

This variable has automatic storage duration (local). The compiler will most likely optimize away this variable as it fills no purpose. But lets assume that such optimization doesn't take place. The variable will then be allocated in runtime, when the scope (block) it resides in is executed and then cease to exist once that scope is finsihed (it goes out of scope). It will be allocated either on the stack or in a CPU register. In other words, at link time this variable only exists as program code, in the form of some assembler instruction saying "push an int on the stack" and then later "pop an int from the stack".

如何对这些不同类型的变量被初始化取决于系统。但通常情况下会有一些code编译器注入main被调用之前。这是一个过于简单化,但对教育学的份上,你能想象你的​​程序实际上是这样的:

How these different kind of variables are initialized depends on the system. But typically there will be some code injected by the compiler before main is called. This is an over-simplification, but for pedagogy's sake, you can imagine that your program actually looks like this:

bss
{
  int uninit_global_var;
}

data
{
  int init_global_var;
}

rodata
{
  5;
}


int start_of_program (void) // called by OS
{
  memset(bss, 0, bss_size);
  memcpy(data, rodata, data_size);

  return main(); 
}

数据:4
BSS:4

data:4 bss:4

与真正的非易失性存储器中的嵌入式系统将工作酷似上述code,而基于RAM的系统可以不同的方式解决了数据初始化部分。 BSS适用于所有系统相同。

Embedded systems with true non-volatile memory will work exactly like the above code, while RAM-based systems may solve the data initialization part differently. bss works the same on all systems.

您可以轻松验证通过运行下面的程序,它们存储在不同的部分:

You can easily verify that they are stored in different segments by running the following program:

char uninit1;
char uninit2;
char init1 = 1;
char init2 = 2;

int main (void)
{
  char local1 = 1;
  char local2 = 2;

  printf("bss\t%p\t%p\n", &uninit1, &uninit2);
  printf("data\t%p\t%p\n", &init1, &init2);
  printf("auto\t%p\t%p\n", &local1, &local2);
}

您将看到UNINIT变量在相邻的地址分配,但在与其他变量不同的地址。同样的,初始化的变量。 局部变量可以在任何地方分配的,所以你得到任何形式奇怪的地址来自这两个结果。

You will see that "uninit" variables are allocated at adjacent addresses, but at different addresses from the other variables. Same with "init" variables. "local" variables can be allocated anywhere so you get any kind of strange address as result from those two.

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

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