0

From Wikipedia:

The BSS segment, also known as uninitialized data, is usually adjacent to the data segment. The BSS segment contains all global variables and static variables that are initialized to zero or do not have explicit initialization in source code. For instance, a variable defined as static int i; would be contained in the BSS segment.

I know the difference between BSS segment and DATA segment.Do a simple test:

root@ubuntu:/tmp# echo "char array[1024*1024*64] = {'A'}; int main() {return 0;}" | gcc -x c - -o data
root@ubuntu:/tmp# ls -lh data
-rwxr-xr-x 1 root root 65M 9月   5 15:55 data
root@ubuntu:/tmp# echo "char array[1024*1024*64]; int main() {return 0;}" | gcc -x c - -o bss
root@ubuntu:/tmp# ls -lh bss
-rwxr-xr-x 1 root root 8.0K 9月   5 15:55 bss

Since uninitialized data is all zero in memory, and in runtime the value is configured in heap section, why do we need it in executable?

Are all the pointers themselves stored in stack section?

Henson Fang
  • 1,177
  • 7
  • 30
  • In your second example, the variable `array` is not on the heap, why do you think that? – Ctx Nov 05 '20 at 08:47
  • By "in exec", I assume you mean "in the executable" Correct – fpmurphy Nov 05 '20 at 08:52
  • 1
    [It doesn't occupy any space in the executable file](https://stackoverflow.com/a/36725653/5267751). – user202729 Nov 05 '20 at 08:55
  • @Ctx,if we use `array` to read file or configure it in runtime,it should be on the heap,I think. – Henson Fang Nov 05 '20 at 08:55
  • 1
    BSS is not the same as heap. [Wikipedia](https://en.wikipedia.org/wiki/Data_segment) – user202729 Nov 05 '20 at 08:57
  • 1
    "and in runtime the value is configured in heap section" Eh? What do you mean? The heap has nothing to do with this. – Lundin Nov 05 '20 at 08:59
  • @MarquisofLorne,Thanks,But I still don't understand why a executable need store a variable information,and in fact it's all zero. – Henson Fang Nov 05 '20 at 09:00
  • It needs to store the existence of the variable, but if its initial value is zero it doesn't need to store that value. – user207421 Nov 05 '20 at 09:00
  • This might be helpful: [What gets allocated on the stack and the heap?](https://software.codidact.com/questions/277536) – Lundin Nov 05 '20 at 09:01
  • That's because an element in BSS has known address at compile time, it resides in a place totally different from any heap allocated variable, so merging the concepts doesn't make any sense. – Jack Nov 05 '20 at 09:02
  • will data (like the BSS) is not intialized by itself, some one did the work for you , init dat/init copy. these procedures run before the glibc, is your question is not clear to me. and as said before heap have nothing to do with BSS. – Adam Nov 05 '20 at 09:03
  • @MarquisofLorne.I really don't know why executable file needs to know variables information,including global,local or static variables.If a variable is constant,we should keep its in the executable.But also the variable self?Isn't that related with the code? – Henson Fang Nov 05 '20 at 09:13
  • Thanks you @Lundin,I will read that question. – Henson Fang Nov 05 '20 at 09:14
  • @HensonFang Because there has to be somewhere to put the variable. It has to be allocated in the executing process at the address specified in the BSS information. – user207421 Nov 05 '20 at 09:51

0 Answers0