Search code examples
c++winapiexecutablecrt

How to make a C++ EXE larger (artificially)


I want to make a dummy Win32 EXE file that is much larger than it should be. So by default a boiler plate Win32 EXE file is 80 KB. I want a 5 MB one for testing some other utilities.

The first idea is to add a resource, but as it turns out embedded resources are not the same as 5 MB of code when it comes to memory allocation. I am thinking I can reference a large library and end up with a huge EXE file? If not, perhaps scripting a few thousand similar methods like AddNum1, AddNum2, etc., etc.?

Any simple ideas are very appreciated.


Solution

  • Use a big array of constant data, like explicit strings:

    char *dummy_data[] = {
        "blajkhsdlmf..(long script-generated random string)..",
        "kjsdfgkhsdfgsdgklj..(etc...)...jldsjglkhsdghlsdhgjkh",
    };
    

    Unlike variable data, constant data often falls in the same memory section as the actual code, although this may be compiler- or linker-dependent.

    Edit: I tested the following and it works on Linux:

    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        int i, j;
    
        puts("char *dummy_data[] = {");
        for (i = 0; i < 5000; i++) {
            fputs("    \"", stdout);
            for (j = 0; j < 1000; j++) putchar('a' + rand() % 26);
            puts("\",");
        }
        puts("};");
        return 0;
    }
    

    Both this code and its output compile cleanly.