Click here to Skip to main content
15,886,857 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I tested these two examples in Code::Block with gcc compiler on Windows x64:

//Example 1
#include <stdio.h>
int global; // <== this is my question

int main(void)
{
    static int i=1000;

    return 0;
}

I compiled this demo with gcc in command line as:
C:\Demo_CPP\mem01>gcc main.c -o mem5-layout
C:\Demo_CPP\mem01>size mem5-layout.exe

I got this memory output:
text    data     bss     dec     hex filename
9708    2216    2440   14364    381c mem5-layout.exe


Now I made small change and intialize this var global=100 as below:
//Example 2
#include <stdio.h>
int global=100;  //<== it is initialized to 100

int main(void)
{
    static int i=1000;

    return 0;
}


now I compile and output this memory layout from command line:
C:\Demo_CPP\mem01>gcc main.c -o mem6-layout
C:\Demo_CPP\mem01>size mem6-layout.exe

I got this memory output:
C:\Demo_CPP\mem01>size mem6-layout.exe
  text    data     bss     dec     hex filename
  9708    2216    2432   14356    3814 mem6-layout.exe


compare these two memory layout, BSS value changed as expected: because var global is initialized to 100, so it is removed from BSS to DS segment,

But why data segment value(2216) does not change?

What I have tried:

from theory, this data segment value shall increase because int global is moved into data segment.

how to explain this data segment value not changing?
Posted
Updated 22-Aug-21 1:48am
Comments
Richard MacCutchan 22-Aug-21 7:20am    
it depends how the compiler generates all the code. You could try looking at the assembler listing to see what is different.

1 solution

Using your code I got the following results:
rjmacc@RJM-Inspiron15:~$ size mem1 
   text    data     bss     dec     hex filename
   1418     548      12    1978     7ba mem1

rjmacc@RJM-Inspiron15:~$ size mem2
   text    data     bss     dec     hex filename
   1418     552       8    1978     7ba mem2
 
Share this answer
 
Comments
Southmountain 22-Aug-21 15:38pm    
on what machine, by what tool, you did this? I did it on my Windows 10 x64 Laptop using Code::Block installation with gcc compilter.
Richard MacCutchan 23-Aug-21 3:28am    
Windows 10 64 bit, using GCC in a WSL window.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900