Fall for code 2.0 . SIGSEGV eror in GANDHIJI AND FOUR MONKEYS

please help me remove this .the program is given desired output on other compiler.
problem code:- FFC219B
contest code:-FFC22019

#include
using namespace std;
#include<stdio.h>

int main()
{
long int T,L[100000],M[100000],tot[100000][100000],sum[100000],i,a1,a2,a3,a4,j;

cin>>T;
for(i=1;i<=T;++i)
{ 
a1=1;
a2=2;
a3=3;
a4=4;


	cin>>L[i]>>M[i];
	
	for(j=1;j<=M[i];++j)
	{
	    tot[i][j]=a1*a2*a3*a4;
		
		a1++;
		a2++;
		a3++;
		a4++;
	}

    sum[i]=0;

	for(j=L[i];j<=M[i];++j)
	{
		sum[i]+=tot[i][j];
	}
	
}

for(i=1;i<=T;++i)
{
	cout<<sum[i]<<"\n";
}

return 0;

}

You have taken a 2D array of 1L*1L, 3 1D arrays of 1L…thats resulting in the SIGSEGV error… Please try to use the minimum number of arrays with minimum size possible.
There’s a space limit on the codechef compiler and you have crossed that limit.

2 Likes

but my program size was 15kb and limit was of 50kb.

That 50kb is actually the size for your code file. This array size is determined at runtime and that is where it exceeded.

1 Like

Static arrays are always allocated in stack memory, so (2d array)1e51e58 + (3 1d arrays)1e583 easily greater than stack memory for a process in almost all OS’s. You should try to keep space minimum as possible. Also allocating in heap through new could accomodate some extra space.Good luck!

1 Like

i am learning to use vectors which will solve this problem of memory allocation . i hope this will help ,but i can’t found any source that tells from beginning to advanced in vectors . one more question can this program be made free from SIGSEGV error with some changes ??

@pkpawan123
Try to optimize your code, use minimum possible arrays and avoid such large 2D arrays.
And when the constraints are upto 10^5 you have to take atleast one extra memory block
ie 10^5 +1
Hope you are getting what I am trying to explain!!

1 Like