Unexpected Size

Thread Starter

Kittu20

Joined Oct 12, 2022
484
Hi everyone,

I'm experimenting with the memory layout of C programs to inspect the different segments. https://www.geeksforgeeks.org/memory-layout-of-c-program/ I'm currently using GCC on a Windows 11 machine with MinGW, and I observed something unexpected in my results.

I'm trying to understand how different types of variables and their initializations affect the memory layout of a C program, specifically looking at the .text, .data, and .bss segments.

Here are the simple C programs I used for my tests:

Empty main function

C:
#include <stdio.h>

int main(void) {
    return 0;
}
Output
Code:
C:\Users\system\Desktop>gcc memory-layout.c -o memory-layout

C:\Users\system\Desktop>size memory-layout.exe
   text    data     bss     dec     hex filename
  14344    1532     112   15988    3e74 memory-layout.exe
Main function with a string:

C:
#include <stdio.h>

int main(void) {
    char *name = "12";
    return 0;
}
Code:
C:\Users\system\Desktop>gcc memory-layout.c -o memory-layout 
C:\Users\system\Desktop>size memory-layout.exe
   text    data     bss     dec     hex filename
  14348    1532     112   15992    3e78 memory-layout.exe
Main function with a slightly longer string:

C:
#include <stdio.h>

int main(void) {
    char *name = "123";
    return 0;
}
Code:
C:\Users\system\Desktop>gcc memory-layout.c -o memory-layout

C:\Users\system\Desktop>size memory-layout.exe
   text    data     bss     dec     hex filename
  14348    1532     112   15992    3e78 memory-layout.exe

I noticed that while the size of the .text segment increased as expected when adding a string to the main function, it remained the same when I changed the string length from "12" to "123". I expected the .text segment size to increase with the longer string, but it didn't

Can anyone explain why the size of the .text segments isn't changing as I expected would be greatly appreciated!

Thanks in advance for your help!
 

Papabravo

Joined Feb 24, 2006
21,314
It is because the initialization constants may not be allocated from the .text segment, or changing the length of a sting by one byte does not affect the allocated sizes. It is used for code. Constnats used for data initialization are handled in some other fashion. You should look at the C initialization function for the target machine to find the answer to your question.
 
Last edited:

Thread Starter

Kittu20

Joined Oct 12, 2022
484
It is because the initialization constants may not be allocated from the .text segment, or changing the length of a sting by one byte does not affect the allocated sizes. It is used for code. Constnats used for data initialization are handled in some other fashion. You should look at the C initialization function for the target machine to find the answer to your question.
I have tried compiling the program with string lengths from 1 to 8 characters

String = "1":

Code:
C:\Users\System\Desktop>size memory-layout.exe
   text    data     bss     dec     hex filename
  14348    1532     112   15992    3e78 memory-layout.exe
String = "12":
Code:
C:\Users\System\Desktop>size memory-layout.exe
   text    data     bss     dec     hex filename
  14348    1532     112   15992    3e78 memory-layout.exe
String = "123":
Code:
C:\Users\System\Desktop>size memory-layout.exe
   text    data     bss     dec     hex filename
  14348    1532     112   15992    3e78 memory-layout.exe
String = "1234":
Code:
C:\Users\System\Desktop>size memory-layout.exe
   text    data     bss     dec     hex filename
  14352    1532     112   15996    3e7c memory-layout.exe
String = "12345":
Code:
C:\Users\System\Desktop>size memory-layout.exe
   text    data     bss     dec     hex filename
  14352    1532     112   15996    3e7c memory-layout.exe
String = "123456":
Code:
C:\Users\System\Desktop>size memory-layout.exe
   text    data     bss     dec     hex filename
  14352    1532     112   15996    3e7c memory-layout.exe
String = "1234567":
Code:
C:\Users\System\Desktop>size memory-layout.exe
   text    data     bss     dec     hex filename
  14352    1532     112   15996    3e7c memory-layout.exe
String = "12345678":
Code:
C:\Users\System\Desktop>size memory-layout.exe
   text    data     bss     dec     hex filename
  14356    1532     112   16000    3e80 memory-layout.exe
As you can see, the sizes remain the same for strings of length 1 to 3 and 4 to 7, only increasing at specific points (lengths of 4 and 8). I expected the size to increase more consistently with each additional character.
8+69

You mentioned that I should look at the C initialization function for the target machine to find the answer to my question. Could you please provide some help on how to do this?
 

nsaspook

Joined Aug 27, 2009
13,567
I have tried compiling the program with string lengths from 1 to 8 characters

String = "1":

Code:
C:\Users\System\Desktop>size memory-layout.exe
   text    data     bss     dec     hex filename
  14348    1532     112   15992    3e78 memory-layout.exe
String = "12":
Code:
C:\Users\System\Desktop>size memory-layout.exe
   text    data     bss     dec     hex filename
  14348    1532     112   15992    3e78 memory-layout.exe
String = "123":
Code:
C:\Users\System\Desktop>size memory-layout.exe
   text    data     bss     dec     hex filename
  14348    1532     112   15992    3e78 memory-layout.exe
String = "1234":
Code:
C:\Users\System\Desktop>size memory-layout.exe
   text    data     bss     dec     hex filename
  14352    1532     112   15996    3e7c memory-layout.exe
String = "12345":
Code:
C:\Users\System\Desktop>size memory-layout.exe
   text    data     bss     dec     hex filename
  14352    1532     112   15996    3e7c memory-layout.exe
String = "123456":
Code:
C:\Users\System\Desktop>size memory-layout.exe
   text    data     bss     dec     hex filename
  14352    1532     112   15996    3e7c memory-layout.exe
String = "1234567":
Code:
C:\Users\System\Desktop>size memory-layout.exe
   text    data     bss     dec     hex filename
  14352    1532     112   15996    3e7c memory-layout.exe
String = "12345678":
Code:
C:\Users\System\Desktop>size memory-layout.exe
   text    data     bss     dec     hex filename
  14356    1532     112   16000    3e80 memory-layout.exe
As you can see, the sizes remain the same for strings of length 1 to 3 and 4 to 7, only increasing at specific points (lengths of 4 and 8). I expected the size to increase more consistently with each additional character.
8+69

You mentioned that I should look at the C initialization function for the target machine to find the answer to my question. Could you please provide some help on how to do this?
What you see is the effect of word access optimizations and restrictions for the hardware target. For string lengths 1 to 3 (add zero string terminator) it takes 32-bit word operands for all sizes (with 32-bit alignment as string size increases) so it allocates memory in 32-bit word size hunks. Byte level 8-bit memory operations, (if possible on the architure) are usually inefficient (even if 8 and 32-bit operations have the same execution time) compared to native word sized operations for a complete software function of several machine operations.
 

Papabravo

Joined Feb 24, 2006
21,314
I have tried compiling the program with string lengths from 1 to 8 characters

String = "1":

Code:
C:\Users\System\Desktop>size memory-layout.exe
   text    data     bss     dec     hex filename
  14348    1532     112   15992    3e78 memory-layout.exe
String = "12":
Code:
C:\Users\System\Desktop>size memory-layout.exe
   text    data     bss     dec     hex filename
  14348    1532     112   15992    3e78 memory-layout.exe
String = "123":
Code:
C:\Users\System\Desktop>size memory-layout.exe
   text    data     bss     dec     hex filename
  14348    1532     112   15992    3e78 memory-layout.exe
String = "1234":
Code:
C:\Users\System\Desktop>size memory-layout.exe
   text    data     bss     dec     hex filename
  14352    1532     112   15996    3e7c memory-layout.exe
String = "12345":
Code:
C:\Users\System\Desktop>size memory-layout.exe
   text    data     bss     dec     hex filename
  14352    1532     112   15996    3e7c memory-layout.exe
String = "123456":
Code:
C:\Users\System\Desktop>size memory-layout.exe
   text    data     bss     dec     hex filename
  14352    1532     112   15996    3e7c memory-layout.exe
String = "1234567":
Code:
C:\Users\System\Desktop>size memory-layout.exe
   text    data     bss     dec     hex filename
  14352    1532     112   15996    3e7c memory-layout.exe
String = "12345678":
Code:
C:\Users\System\Desktop>size memory-layout.exe
   text    data     bss     dec     hex filename
  14356    1532     112   16000    3e80 memory-layout.exe
As you can see, the sizes remain the same for strings of length 1 to 3 and 4 to 7, only increasing at specific points (lengths of 4 and 8). I expected the size to increase more consistently with each additional character.
8+69

You mentioned that I should look at the C initialization function for the target machine to find the answer to my question. Could you please provide some help on how to do this?
One place to look is at the make file that is used to build your program. Alternatively the link editor map should have the name of the module with the C-initialization code. Once you know the name of the object file you need to find how it is built and what it actually does. You have to poke around in your compiler setup until you find the answer. I could be more help if I was sitting on your shoulder like Long John Silver's parrot, but I'm not, and you're just going to have to put on your Big Boy pants and figure this out for yourself. It is what I had to do.
 
Top