Error: Total size of array must not exceed 0x7fffffff bytes

前端 未结 2 779
遥遥无期
遥遥无期 2021-01-05 23:41

I have a small C++ program that requires a large 2d array.

int distanceArray[282][9900000];

I am aware that a standard 32 bit console appli

相关标签:
2条回答
  • 2021-01-05 23:59

    As suggested by MSalters, an std::vector<std::vector<int>> was definitely the way to go.

    For anyone who is still having this problem, here is how I initialized it:

    std::vector<std::vector<int>> distanceArray(282, std::vector<int>(9000000, -1));
    

    9,000,000 columns are created within every row of 282 items, and each value is initialized to -1 at the start.

    Thanks to everyone who commented for the help!

    0 讨论(0)
  • 2021-01-06 00:02

    The 64-bit PECOFF executable format used on Windows doesn't support creating executables that have a load size of greater than 2GB so you can't create statically allocated objects bigger than that. You run into a similar problem if you try create such an object on the stack using an automatically allocated object.

    One possible solution would be to dynamically allocate the object:

    int (*distanceArray)[9900000] = (int (*)[9900000]) calloc(282, 9900000);
    

    Or if you want it more C++'ish and don't need it to be zero initialized like a statically allocated array would be:

    int (*distanceArray)[9900000] = new int[282][9900000];
    
    0 讨论(0)
提交回复
热议问题