简体   繁体   中英

Why .bss size decrease when adding an initialized static variable?

I understand that the default alignment of .bss is 8 bytes for GCC as mentioned in this question Why the int type takes up 8 bytes in BSS section but 4 bytes in DATA section

So with this program:

int main(){

    return 0;
}

I have something like this:

   text    data     bss     dec     hex filename
   1418     544       8    1970     7b2 test

When I add an static variable with initialization to increase .data (and it does):

static int var = 255;

int main(){

    return 0;
}

I see that the size of .bss also decrease 4 bytes:

  text     data     bss     dec     hex filename
  1418      548       4    1970     7b2 test

Please tell my why ?

The .bss has the size of the uninitialized global variables. These will be initialized to zero upon loading of the program.

If you initialize a global variable to something other than zero, it will no longer be in the .bss but will be in the .data segment. The data segment contains all initialized global variables (with their initial value).

Hence, the size of the .bss decreases and the size if the .data increases.

可能是因为 bss 的初始化保留了基本的 8 个字节,引入了全零所以内存管理器不会读取垃圾,但是当我们最终创建一个适合 4 个字节的变量时,bss 减少了(以占用更少的可能记忆)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM