How to solve the problem of insufficient stack memory for large arrays in c++

In C++, we can create an array directly in the following way:

const  int N = 6 ; 
const  int Nx = 10 0 ; 
const  int Ny = 10 0 ;

double phi[N][Nx][Ny];   
double phi_b[N][Nx][Ny];

However, if the above Nx and Ny are relatively small, it is okay to say that once Nx and Ny are very large, an error will be reported and the compilation will fail.

 

To solve this problem, we can use the following methods to solve this problem:

1. In the link tab in the Project setting of VC, open the stack a little bigger (the default is 4M in windows)   

2. Declared as global or static, these two variables do not push the stack, you can open as much as you want. Obviously, the global variable itself can have more memory (this method can work immediately, which is good)

3. Use the method of dynamically allocating array memory:

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

4. Use vector as follows:

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

 

Among the above methods, dynamic allocation of array memory is used, which is a good method for heap storage, but the cost is high; and the method of adding static has the lowest cost.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324940670&siteId=291194637