Reputation: 9
I have created 2 C programs in Ubuntu(Linux 2.6) as below
1.c
----
main()
{
}
2.c
----
#include<stdio.h>
main()
{
int a[500];
float f[1000];
double d[100000];
int i = 0;
for(i = 0;i < 10000;i++); // Intentional ;
for(i = 0;i < 10000;i++); // Intentional ;
for(i = 0;i < 10000;i++); // Intentional ;
for(i = 0;i < 10000;i++); // Intentional ;
if(1)
{
}
else
{
}
switch(1)
{
}
while(1);
}
After separately compiled and created executable files,I checked the size of both the executables.To my surprise the size of both the executables were same(7099 bytes).
However the size of object file differs.
Someone please explain me why the executable size was same for program 1.c and 2.c.Program 2.c should have used more space and executable size should have got increased right? How linker links C keywords(like int,float,while,if..) and creates executable file?
Thanks a lot
Upvotes: 0
Views: 167
Reputation: 9
Able to find a convincing solution for the asked question. Below is the analysis and observations regarding this.
In the code 2.c, I added many statements of "for(i = 0;i < 10000;i++); // Intentional ;". At some point, observed that the size of the executable of 2.c got increased by 4096 bytes. It seems like, linker by default assigns 4k of memory for code section as mentioned in the linker script for PAGE Alignment. For x86, this size is 4k(COMMONPAGESIZE). Only after the code size exceeds more than 4k, a new 4k of memory gets assigned for the code section. That's why when more "for(i = 0;i < 10000;i++); // Intentional ;" statements were added, the executable size got increased.
Upvotes: 0
Reputation: 3298
I believe that's related to optimizer. But, for linker I suggest reading the points below since you showed curiosity about linkers. Reading these would help any C & C++ programmer. Knowing what linking actually means is an important knowledge.
For more info:
Note: This may not be what you were looking for but researching and learning what you're looking for by yourself will make what you learned last longer.
Upvotes: 1