C++ to solve the problem of large array stack memory insufficient method analysis

  • 2020-06-03 07:54:46
  • OfStack

This article illustrates how C++ solves the problem of large array stack memory. To share for your reference, specific as follows:

In c++, we can create an array directly by:

const int N = 6;
const int Nx = 100;
const int Ny = 100;
double phi[N][Nx][Ny];
double phi_b[N][Nx][Ny];

However, if the above Nx and Ny are relatively small, it is easy to say that once Nx and Ny are large, an error will be reported, resulting in compilation failure.

To solve this problem, we can adopt the following methods:

1. Open the stack by 1 point in the link option card in VC Project setting (4M is the default in windows)

Declare as global or static Obviously, global variables can have more memory on their own (this method works immediately, but it is still good).

3. Dynamic array memory allocation method:

int *A = new int[90000];
delete A;

4. vector is used as follows:

#include <vector>
using namespace std;
void main()
  vector<int> A(90000);
  A[0] = 1;

In the above methods, using dynamic array memory allocation, which USES time heap storage, is still a better method, but the cost is high; Instead of adding static Is the least expensive method.

I hope this article is helpful for C++ programming.

Related articles: