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
Output
Main function with a string:
Main function with a slightly longer string:
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!
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;
}
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
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
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!