0

I know that bss segment contains uninitialized variables which will be given 0 as initial value,but why we don't just use the data segment to store them?

I read that it speed up execution because the os loader initialize all these variables at once by calling memset, however, I'm not very convinced because the loader can call memset to initialize them in data segment also!

Guy Avraham
  • 3,482
  • 3
  • 38
  • 50
Amine Hajyoussef
  • 4,381
  • 3
  • 22
  • 26
  • How would the loader know what part of the data segment contains the data that needs to be initialized? Often it's actually not the loader that does the clearing of that memory, but a routine that is linked with the application and runs before the calling of `main` (what is known as the C runtime, or CRT). – Some programmer dude Jan 20 '12 at 08:06
  • I found this answer very informative: https://stackoverflow.com/a/46376748/1971003 – Guy Avraham Jul 21 '23 at 13:45

1 Answers1

3

Because you might have a huge set of uninitialized values. You will have to fill the binary with zeros (or any value, for that matter, but something has to be there) if you were to use the data section for that purpose. Imagine adding a megabyte of zeros to the executable file...

Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
  • Short version: it's to save space in the executable file. – ams Jan 20 '12 at 13:11
  • i understand why the compiler should group them in the executable, but why the os loader store them in a separated logic segment in the memory!!! – Amine Hajyoussef Jan 21 '12 at 12:10
  • @Pindexis I don't follow. Surely when you load, you cannot easily merge the two segments. You naturally load them into two different locations. That's not a big deal anyway. – Mehrdad Afshari Jan 21 '12 at 12:12