#include <stdio.h>
const int str[1000] = {0};
int main(void)
{
printf("arr is %d\n", str[0]);
return 0;
}
It has the following output:
[-exercises/adam/stack2]:size a.out
text data bss dec hex filename
5133 272 24 5429 1535 a.out
While:
#include <stdio.h>
static int str[1000] = {0};
int main(void)
{
printf("arr is %d\n", str[0]);
return 0;
}
It has the following output:
[-exercises/adam/stack2]:size a.out
text data bss dec hex filename
1080 4292 24 5396 1514 a.out
When the array is not initialized, it again goes into the text segment for "const" and BSS for "static".
The variable is global and must be accessible from anywhere in the executable file from which it is (due to the lack of "static"), but given its variable, I don’t know why it is placed in the text segment instead of the data segment
source
share