2

This was compiled on 64 bit VS C++ 2015. std::bad_alloc occurs where x is specifically 1120

static std::vector<std::vector<std::vector<double>>> g_damagefunction;
static std::vector<std::vector<double>> g_has_damagefunction;
static std::vector<double> null_v_double;
static std::vector<bool> null_v_bool;
static std::vector<std::vector<double>> null_vv_double;
int main(){
for (int x = 0; x < 4400; x++) {
    std::cout << x << '\n';
    g_damagefunction.push_back(null_vv_double);
    g_has_damagefunction.push_back(null_v_bool);

    for (int y = 0; y < 2000; y++) {
        g_damagefunction[x].push_back(null_v_double);
        g_has_damagefunction[x].push_back(false);
        for (int i = 0; i < 41; i++) {
            g_damagefunction[x][y].push_back(0.0);
        }
    }
}
}
Carlos J
  • 23
  • 5
  • I think it is used too much memory. You should be able to see how much memory is used when it reached the crash point. – Bryan Chen Dec 04 '16 at 20:25
  • 1
    [No repro](http://coliru.stacked-crooked.com/a/fafd1d273a5f4e1f) – πάντα ῥεῖ Dec 04 '16 at 20:26
  • @πάνταῥεῖ it ends at 1040. I got expired when I was trying it on coliru. – Bryan Chen Dec 04 '16 at 20:27
  • I fixed the code a little bit (I was treating a double as a bool). I looked at "No repro" and it seems to only go to 1000-something – Carlos J Dec 04 '16 at 20:28
  • @BryanChen I've also got expired at the 1st run at coliru. No repro though. – πάντα ῥεῖ Dec 04 '16 at 20:28
  • I'm looking at memory management, and, the computer has over 24GB of memory, and the program itself only uses like at the time of crashing 700MB (so it's definitely not a memory limit problem) – Carlos J Dec 04 '16 at 20:29
  • Exception thrown at 0x763C5B68 where x = 1120 – Carlos J Dec 04 '16 at 20:31
  • I get `std::bad_alloc` at 4096 when I run the program in a 32-bit chroot. I have no problems when running on a 64-bit system. Are you sure you're running in (and have compiled for) a 64-bit environment? – mindriot Dec 04 '16 at 20:36
  • I am absolutely 100% sure, Visual Studio is currently configured for 64-bit Release. – Carlos J Dec 04 '16 at 20:40
  • I created a fresh Visual Studio project, and it worked. Had something to do with sharing a directory with another program (I copied an older project's folder and rewrote the program) – Carlos J Dec 04 '16 at 20:50

1 Answers1

2

bad_alloc here means that you are running out of memory. The size of the double elements in g_damagefunction vector will take (4400 x 2000 x 41 x sizeof(double)) bytes which amounts to about 2.68 GBs of your memory. If you add to it the amount of memory the vectors themselves take up, you can clearly see that either your computer's RAM is insufficient for your program's requirements, or your program is using too much memory. I suspect the latter to be the case.