Abstract problem: I have some code in C. After compilation the executable has 604 KB. I'd like it to be bigger, let's say 100 MB.
How to achieve this?
I could declare a string to increase binary size, but is there any more scalable solution. That is I'd like to increase compiled size by N bytes without increasing source code by N bytes.
char* filler = "filler"; // increases compiled size only by few bytes
Use case: I'm developing firmware, and testing remote firmware upgrade feature. I'd like to see how it will behave when firmware is big and heavy.
in Go 1.2, a decision was made to pre-expand the line table in the executable file into its final format suitable for direct use at run-time, without an additional decompression step. In other words, the Go team decided to make executable files larger to save up on initialization time.
typically, compiled code is smaller than the source code it is compiled from.
One reason executables can be large is that portions of the C++ runtime library might get statically linked with your program.
Again go to the "Project Settings", "Link" tab. Type /ALIGN:4096 in the Project Options edit box. Press OK and rebuild your project. The size is further reduced to 3K.
This creates a 100MB executable when compiled with gcc:
#include <stdio.h>
#define SIZE 100000000
char dummy[SIZE] = {'a'};
int main(void){
dummy[SIZE-1] = '\n';
if(dummy[0] == 'a')printf("Hello, bloated world");
return 0;
}
By defining the array outside of main
you don't blow the stack. By using the array, gcc doesn't optimize it away.
GCC specific variant:
char dummy[100*1024*1024] __attribute__((used)) = { 77 };
Applying the attribute 'used', you do not need to touch it any more to prevent it from being optimised away. Still, an non-all-null initialiser must be applied as in John Coleman's solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With