This is what I had earlier
#include <stdio.h>
int main()
{
return 0;
}
And I got this
rohit:~/workspace/C $ size const
text data bss dec hex filename
1122 276 4 1317 525 const
After making the below change, the data section size increases by 4.
int main()
{
printf("Hello World!!");
return 0;
}
rohit:~/workspace/C $ size const
text data bss dec hex filename
1112 280 8 1400 578 const
What's the reason for this behaviour. I am using Ubuntu on x86. gcc is the compiler.
TL;DR: Every used string uses the memory for itself, as well as sizeof(char*)
additional memory for a pointer to its first character.
You're probably on a 32bit architecture, or at least compile your program for that one. Four bytes are exactly one pointer on that architecture, so let us have a look at the assembler ( gcc -S
):
.file "main.c"
.section .rodata
.LC0:
.string "Hello World!!"
There's a .rodata
entry, namely "Hello World!""
as a .string
. That's a const char *
, which takes four bytes on a 32bit x86 arch. If we remove that string, the .rodata
section stays empty. The string is gone, therefore we can get rid of the pointer. Note that the pointer itself is not read-only. That's why you end up with an entry in data
to begin with.
You can check this easily with gcc -Os main.c
. The following programs should have the same size
output (and result in almost the same assembler code):
#include <stdio.h>
int main()
{
printf("hello world!");
return 0;
}
#include <stdio.h>
int main()
{
const char * mystring = "hello world!";
printf(mystring);
return 0;
}
After all, we need the pointer for printf
( $.LC0
):
movq $.LC0, -8(%rbp)
movq -8(%rbp), %rax
movq %rax, %rdi
movl $0, %eax
call printf
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.