1

I wanted to understand how a "C" program runs and store the data in machine. So I looked into Memory Layout of C from here and I followed the same instructions in my machine which is 64-bit.

First when I wrote the program (main has only return 0;) and used the size command for the executable file: it showed a lot of difference in both text and data segments.

text    data    bss     dec     hex     filename
10648   2400    2640    15688   3d48    33.exe

But in the website mentioned above it showed:

text    data    bss     dec     hex     filename
960     248     8       1216    4c0     memory-layout

First Question: What are the factors (hardware/software) that are responsible for the memory allocation?. And what does dec in the layout refer to? /Question ends here

But first I ignored this and started declaring the variables (global and static) to see where they are being stored. And I was facing a problem in this phase.

for this code:

#include <stdio.h> 
int global;  
int main(void) {
    //static int x;
    return 0; 
} 

I got output as:

text    data    bss     dec     hex     filename
10648   2400    2656    15704   3d48    33.exe

That is because I declared (uninitialised) a global variable and that is why 16 bytes (int-64bit) of memory block has been added to the bss so it became 2656 from 2640 (first example) I understand this.

Q2: But when I add the static int x it is not adding the memory block to bss anymore. Is this expected?

text    data    bss     dec     hex     filename
10648   2400    2656    15704   3d48    33.exe 

Q3: And finally when I initialize the global variable with 20, data got incremented (expected) and dec also got incremented why?

text    data    bss     dec     hex     filename
10648   2416    2656    15720   3d48    33.exe

I know I asked many questions here, but I wanted to know exactly how this memory management works in C.

Arigato:)

chqrlie
  • 131,814
  • 10
  • 121
  • 189
heeat
  • 128
  • 2
  • 9
  • 1
    Ummm. `dec 15688` is `hex 3d48` (no mystery there) Both global and those declared `static` have static storage duration and are initialize to zero by default. (no clue on 3Q). – David C. Rankin Feb 20 '19 at 06:49
  • Small differences in compiler flags and compiler versions and operating systems will lead to large differences in the generated executable. Whatever documentation or tutorial you read, all its examples will be just that: *examples*. And what `size` is telling you have nothing to do with the in-memory layout of a running program, only information about the executable file itself. – Some programmer dude Feb 20 '19 at 06:50
  • Quote: "...i wanted to know exactly how this memory management works in C" Well, that can't be answered as the C standard by itself does **not** specify such things. Therefore it may differ from system/setup to system/setup. Only when you give an exact specification of your system/setup it will be possible to answer. – Support Ukraine Feb 20 '19 at 06:53
  • And nothing in this question is about the runtime memory of a program or C memory management, all it's about are the segments of the executable file *on disk*. On-disk executable file segments really have no relation to runtime memory management, and haven't had the last couple of decades. – Some programmer dude Feb 20 '19 at 06:53
  • 1
    `"And finally when i initialize the global variable with 20, data got incremented (expected) and dec also got incremented why?"` (both `dec` and `hex` were incremented because they represent the sum of `text + data + bss`) – David C. Rankin Feb 20 '19 at 06:56
  • A more fine-grained explanation can be gathered by using `objdump`, e.g. `objdump -D -M "intel-mnemonic" path/to/your/exe`. It will show detailed contents of each of the `.text .bss & .data` sections. – David C. Rankin Feb 20 '19 at 07:02
  • See this example: https://stackoverflow.com/questions/52855674/a-program-uses-different-regions-of-memory-for-static-objects-automatic-objects – Lundin Feb 20 '19 at 07:56
  • As for dec, it is simply the total size of all code and data expressed in decimal format. And hex is the same, but hex format. – Lundin Feb 20 '19 at 07:58

2 Answers2

1

What are the factors (Hardware/Software) that are responsible for the memory allocation?. And what does dec in the layout refer to

Your program is divided into many sections when it runs. stack contains the local variables. bss data contains the uninitalized global variables. initialized data contains the initialized global variables. text contains your text (code). dec is the sum of text, bss , data

But when i add the static int x it is not adding the memory block to bss anymore.is it expected?

Keep it uninitialized then it will add.

Before adding static variable:

   text    data     bss     dec     hex filename
   1099     544       8    1651     673 a.out

After adding static variable:

   text    data     bss     dec     hex filename
   1099     544      16    1659     67b a.out

And finally when i initialize the global variable with 20, data got incremented (expected) and dec also got incremented. Why?

Because dec is the sum of text, data , bss

Syed Waris
  • 1,056
  • 1
  • 11
  • 16
  • What are the 3 digits in hex? are they the last 3 digits of the hexcode i.e the address where these memort blocks are stored or is there any other meaning. – heeat Feb 20 '19 at 08:00
  • The 3 characters in `hex` do not specify memory location. It other meaning. They signify the memory area used by your process. They are just hexadecimal equivalent of `dec`. – Syed Waris Feb 20 '19 at 08:47
0

Here is a quick summary:

  • executable code, string literals and constant global variables are loaded to the text segment, which is made read-only in memory by the OS program loader on systems that support it.

  • global initialized modifiable objects and local initialized objects declared with static storage are loaded into the data segment.

  • uninitialized modifiable global objects and uninitialized local static objects are allocated in the bss segment, which is not present in the executable file.

  • other local objects with automatic storage are carved dynamically from the stack segment upon function entry of sometimes on the fly when they come into scope.

  • the dec column gives the total size of the program code and data: the sum of text, data and bss. This does not include memory allocated at startup time, stack space, nor data allocated dynamically from the heap or using other OS specific methods.

  • hex is the hexadecimal representation of dec.

These values vary depending on the operating system, compiler, compiler settings and libraries. The website shows the output on a system running CentOS, possibly in 32-bit mode. Note how the same program produces much larger values for all segments mostly due to a different C library startup code. The main function itself should use from a few bytes to at most a few dozens depending on compiling options.

The size of the executable file is different from dec because it does not contain the bss data but contain other information regarding how to load the program: the sizes and placement of the various segments, the references to dynamic libraries and the symbols to link from them, debug information, etc.

When you add a global variable, bss is increased if it is uninitialized, otherwise data increases. When you add a local variable, the same happens if it is static.

Note however that alignment issues may affect how the data is added to a segment and optimisation settings may affect whether new objects are actually added to the executable. If the compiler can determine that an object is never used, it might ignore it completely.

Modern operating systems handle many more segments and intricacies about executable programs, the above is a simplistic approach that models the memory layout of executable programs in the early Unix systems during the 1970s and 1980s. The names predate Unix by almost 2 decades. More information can be displayed with the objdump and nm utilities.

You can read a detailed explanation about the standard Unix segments names here:

text: https://en.wikipedia.org/wiki/Code_segment

data: https://en.wikipedia.org/wiki/Data_segment

bss: https://en.wikipedia.org/wiki/.bss

chqrlie
  • 131,814
  • 10
  • 121
  • 189