simple array cause exception

前端 未结 4 1179
孤独总比滥情好
孤独总比滥情好 2020-12-12 02:46
#include \"stdafx.h\"

int _tmain(int argc, _TCHAR* argv[])
{
    float x[1000][1000];

    return 0;
}

I get \" First-chance exception at 0x013416

4条回答
  •  忘掉有多难
    2020-12-12 03:26

    Your array is simply too large to fit on the stack. You don't have enough stack space for 1000 * 1000 elements.

    You'll need to allocate your array on the heap. You can do this using the new keyword, but an easier way is to just use std::vector.

    std::vector > floats(1000);
    for (unsigned i = 0; i != floats.size(); ++i) floats[i].resize(1000);
    

    This will give you a two-dimensional vector of floats, with 1000 elements per vector.

    Also see: Segmentation fault on large array sizes

提交回复
热议问题