Skip to content

Instantly share code, notes, and snippets.

@teknogeek
Created April 25, 2016 18:15
Show Gist options
  • Save teknogeek/de00ba2f9d627352b55c77c4170d2a23 to your computer and use it in GitHub Desktop.
Save teknogeek/de00ba2f9d627352b55c77c4170d2a23 to your computer and use it in GitHub Desktop.
Snippet that causes g++ to hang and use gigabytes of ram (no template/include loops)
#include <stdio.h>
// tested on gcc-4.9.3 on linux gentoo amd64
// warning: compiling this will eat gigabytes of ram really fast so keep
// your CTRL-C ready
// compile with --std=c++14
struct v2f {
float x=0, y=0; // must be =0 to reproduce (probably works with other vals)
};
namespace {
struct foo {
// declaring the array like this somehow bypasses the fact that
// v2f cannot be initialized with {0}
static const size_t max_objects = 0xFFFF;
size_t num_objects; // this is not necessary, just wanted to check if it still did it when not accessing bars
v2f bars[max_objects * 255]; // the larger this array is, the longer it hangs for and the more memory it uses
};
foo f{0}; // must be initialized with {0} to reproduce
}
int main() {
printf("%ld\n", f.num_objects);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment