I am testing recursion, however when I have an array with more than 150000 elements segmentation error occurs. What can be the problem?
#include <iostream> using namespace std; void init ( float a[] , long int n ); float standard ( float a[] , long int n , long int i ); int main() { long int n = 1000000; float *a = new float[n]; init ( a , n ); cout.precision ( 30 ); cout << "I got here." << endl; cout << "Standard sum= " << standard ( a , 0 , n - 1 ) << endl; delete [] a; return 0; } void init ( float a[] , long int n ) { for (long int i = 0 ; i < n ; i++ ) { a[i] = 1. / ( i + 1. ); } } float standard ( float a[] , long int i , long int n ) { if ( i <= n ) return a[i] + standard ( a , i + 1 , n ); return 0; }
I got here. Segmentation fault (core dumped)