2D array

I want to have a big enough 2D array without using dynamic variables. My system just can create [80][80] array! while I need much bigger one, please help!

#include <iostream>
using namespace std ;
class a
{
public:
a(){}
virtual ~a(){}
static int x[20];
};
int a::x[20]={1,2,3,4};
int main()
{
for(int i=0;i<4;i++)
cout << a::x[i] <<endl ;
return 0;

}
static
static int x[80];
int a::x[80]={1,2,3,4.......};
static 2D [ 80 ] [80];
Ask questions

Baidu know within ten minutes will ask to download immediately

How C ++ defines an array of more than one million in length

I want to solve a random number of 1 million numbers and then sort them from small to large
Need to define an array of 1 million length, the compiler can be passed, but a running program will stop, how to solve?
The code is as follows:
#include <iostream>
#include <algorithm>
using namespace std;
int main () {
int a, b;
int list [1000000];
cin >> a;
for (b = 0; b <a; b ++)
cin >> list [b];
sort (list, list + a);
for (b = 0; b <a; b ++)
cout << list [b] << endl;

return 0;
}

cmdjs 2013-11-22

Professional answer

Array declaration within the function, belonging to local variables, stored on the stack,
Look at the memory size occupied by the array: 1000000 = 1000 * 1000 and then multiply the int-type data length
1000 * 1000 * 4byte is about equal to 4M,
And the default memory space stack 1M or so, it will lead to memory overflow
To solve this problem, you can declare the array in the global storage area or heap can be
Method 1: declare as a global variable
Method 2: stored on the heap
your compiler should have settings to increase the max size of the stack, but it will have an upper limit that you cannot exceed. 80x80 is very small** on a modern machine, so you should be able to beat that by upping the setting. On visual studio it is just a box and you can type in some value you want. On other compilers it is probably a command line switch, but I don't know which one.

There is no reason not to do this dynamically, or better still, in a vector. The stack was never meant for large objects; you need some of it for your parameters and function call data etc.

**assuming it is 80x80 of some POD type, and not a fat class.
Last edited on
@Hamidpooran

Not quite sure what you want, but if you have fixed-size arrays then I think they will get put on the stack, and that's rather a limited amount of memory (although you might be able to increase it). You need to be using the heap.

If you don't enjoy using new and delete then you could use a vector of vectors (or an equivalent 1-d version: see below). Or there are other containers like std::array or std::valarray. All these have the advantage that their size can be decided at run time (so they won't use copious amounts of memory even if your problem turns out to be smaller) They can also be resized mid-run if needed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <vector>
using namespace std;

// Convenient aliases
using vec    = vector<double>;
using matrix = vector<vec>;                 // array of (row) vectors

// Prototypes
int index( int i, int j, int nj );
void print( const vec &V, int nj );
void print( const matrix &A );

//======================================================================


int main()
{
   int M, N;

   cout << "Create M(rows) x N(cols) array. Enter M N : ";
   cin >> M >> N;
   
   matrix mat2d( M, vec( N ) );             // Using a vector of vectors
   vec mat1d( M * N );                      // Using an indexed 1-d array

   // Populate both with the same values
   for ( int i = 0; i < M; i++ )
   {
      for ( int j = 0; j < N; j++ )
      {
         double value = 10.0 * i + j;
         mat2d[i][j]         = value;
         mat1d[index(i,j,N)] = value;
      }
   }

   cout << "\nmatrix form:\n";   print( mat2d );
   cout << "\nvector form:\n";   print( mat1d, N );
}


//======================================================================


int index( int i, int j, int nj ) { return i * nj + j; }


//======================================================================


void print( const vec &V, int nj )
{
   int ni = V.size() / nj;
   for ( int i = 0; i < ni; i++ )
   {
      for ( int j = 0; j < nj; j++ ) cout << V[ index(i,j,nj) ] << '\t';
      cout << '\n';
   }
}


//======================================================================


void print( const matrix &A )
{
   int ni = A.size();
   int nj = A[0].size();

   for ( int i = 0; i < ni; i++ )
   {
      for ( int j = 0; j < nj; j++ ) cout << A[i][j] << '\t';
      cout << '\n';
   }
}

//====================================================================== 


Create M(rows) x N(cols) array. Enter M N : 4 7

matrix form:
0       1       2       3       4       5       6       
10      11      12      13      14      15      16      
20      21      22      23      24      25      26      
30      31      32      33      34      35      36      

vector form:
0       1       2       3       4       5       6       
10      11      12      13      14      15      16      
20      21      22      23      24      25      26      
30      31      32      33      34      35      36 
Last edited on
Use static storage duration.

1
2
3
4
5
6
7
#include <iostream>

int main() 
{
    static double arr[8'000][8'000] {} ;
    std::cout << sizeof(arr) << '\n' ;
}

http://coliru.stacked-crooked.com/a/b93387582a046627
Topic archived. No new replies allowed.