For program from http://bellard.org/mersenne.html GCC produces ~130MB executable. Why?
For program from http://bellard.org/mersenne.html GCC produces ~130MB executable. Why?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Try changing
t[1<<25]={2}tot[1<<25]and the size of the *executable** will drop down to 7.3 K. (Needless to say, you won’t get the right results)If it was just
t[1<<25], it wouldn’t have taken any space at all.The catch here is that the array is being initialized (first element=2, the next 2^25-1 elements all 0), and the global array gets placed in the data segment only because it is initialized.
Generating the assembly for the 2 versions and examining the difference makes it even more clear:
As we can notice, in the original version, the assembler is directed to generate 2^27 (134217728 ) bytes in the data segment. So it becomes a part of the object file itself. ( You can generate assembly by compiling with
-Sswitchgcc -S -fverbose-asm t1.c)But why 129 MBs?
1<< n= 2^n (1 left shifted n times). => 1<<25=2^25. now 1 Integer= 4 bytes =2^2 bytes => 2^25 Integers=2^27 bytes=2^7 * 1 M bytes= 128 MBsFor more details, see :
Question.
segment
*Note 1: It is an object file in strict terms.
Note 2: As pointed out in the comments, It may also be noted that the total size of the process (program in execution) will be 129Mb even if the executable is of 7.3K. (The memory will be allocated once the program starts executing). You can see the memory usage of your program by using the
topcommand.Note 3: It is worth emphasizing that this holds only because t is global. Allocation for data local to a function still happens at runtime on the stack. So if
twas local, the object file would’ve taken 7.3K only.Note 4: Initialized
staticlocal variables, like initialized globals, are also kept in thedatasegment. Astaticglobal is same as a global except that you are limiting the scope of the variable to the current file only.