Is there a limit of array size?

Write your question here.

This sample of code works fine.
1
2
3
4
5
6
 int main() {
    const int m = 10000000;
    int n = 10;
    int arr[100];
    return 0;
}


But this one crashes with an exception error code c00000fd which means stack overflow
1
2
3
4
5
6
7
int main()
{
    const int m = 10000000;
    int n = 10;
    int arr[10000000];
    return 0;
}

is there any way to address int array of 10^7 elements? i need it to solve my problem
There's a very definite limit to the size of an array you can create on the stack as a local variable.
Depending on your OS/Compiler, the usual range for your average desktop environment is a stack limit between 1MB and 8MB.

> is there any way to address int array of 10^7 elements? i need it to solve my problem
Just do this,
int *arr = new int[1000000];
but don't forget this when you're done
delete [] arr;

Alternatively, an array who's scope is main, but isn't on the stack.
static int arr[10000000];
> is there any way to address int array of 10^7 elements? i need it to solve my problem

Try making an array with static storage duration.

1
2
constexpr std::size_t ARRAY_SZ = 10'000'000 ;
static int arr[ARRAY_SZ] ;


If that fails on your implementation, use std::vector<>
https://cal-linux.com/tutorials/vectors.html
Thanks for help, i've got another question
i've allocated array on heap using *arr = new int
i want also create vector of vectors<int> and it behaves exactly like array stackoverflow and crashes
std::vector<int> buckets[10000000]; //crash
then i access to vector with
buckets[index].push_back(arr[i]); // worked when there were no problems with creating buckets with smaller size

how can i convert it to heap to let me create that huge vector of vectors<int>?
and how to add ints to this new created vector on heap?
thanks in advance
Something like this:

1
2
3
4
5
6
7
constexpr std::size_t NROWS = 10'000'000 ;
std::vector< std::vector<int> > buckets(NROWS) ; // NROWS empty vectors

// add/modify (place 99 in) buckets[100][23]
auto& row = buckets[100] ; // get a reference to the 100th vector
if( row.size() < 24 ) row.resize(24) ; // resize it if required
row[23] = 99 ; // place 99 in col 23 


If many of the elements in this huge matrix are zero (or have the same value),
consider using a sparse matrix representation.
https://en.wikipedia.org/wiki/Sparse_matrix
Also compile as 64bit and not as 32bit.
some compilers also have flags to make the stack size larger. It still has an upper limit, but you may be able to extend it if you were on the edge of the default.
Topic archived. No new replies allowed.