0

I am having speed issues with the C++ standard arrays , so I am testing using the stl::array for computation, to see how fast it is.

I define a two dimensional array of the struct PAPoint (see below) and attempt to create a 2D array of it. It compiles ok, but when I try to run I get a Stack overflow (literally) exception. 2D arrays with size 200x200 run ok but not 500x500. Is there a solution? Is the stack really that small?

Thank you

#include <iostream>
#include <array>
#include <cmath>

#define PAWIDTH 500 //STACK OVERFLOW FOR LARGE NUMBERS
#define PAHEIGHT 500

struct PApoint{
    float elpotential = 0;
    bool iselectrode = false;
}
;

int main()
{
    std::cout << "SpeedTestEMLaplaceSolver.cpp using 2D std::array of PApoint\n";
    std::array< std::array<PApoint, PAWIDTH>, PAHEIGHT> myPAArray; //Array is allocated in the stack so large arrays can trigger stack overflow
    //Gives error in chkstk.asm
    /*
    Unhandled exception at 0x00827659 in SpeedTestEMLaplaceSolver.exe: 0xC00000FD: Stack overflow (parameters: 0x00000000, 0x00402000). occurred
    */
}
3
  • 2
    Allowing for 8-byte alignment of the elements, your array is 500 x 500 x 8 bytes in size = 2MB. The stack limit on Windows is 1MB. You'll have to use a std::vector (and why not). Commented Oct 17, 2020 at 15:03
  • 1
    Thanks Paul. First I didn't know that the std::vector uses the stack, neither I knew there was such a small limit. Commented Oct 18, 2020 at 17:44
  • 1
    std::vector doesn't use the stack. std::array does, hence your problem. Commented Oct 18, 2020 at 19:44

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.