my code is giving segmentation error when i try to submit it on gfg
please help
my code is giving segmentation error when i try to submit it on gfg
please help
Hey @gulatigarvita08
I think u shared the incomplete code , because long getMaxArea(long long arr[], int n) function is missing in it,please reshare the complete code
oh sorry
Hey @gulatigarvita08
Mentioned the changes in comments : https://ide.codingblocks.com/s/370880
You are getting segmentation because of static allocation
Max size arrays u can create inside function statically is close to 10^6 so sometimes it gives segmentation
but 1<=n<=10^6
so why long long
n is array size but array elements can be of order 10^18 that is why long long
that array i took as long long
but lb and rb are just storing idexes … so ig it can have static allocation
Oh yes ,sorry u can use int for them
but then why my code is giving segmentation error
Irrespective of int array or long long array
sorry could’nt understand
when do we statically allocate ??
int arr[n] // this is static allocation
int*arr=new int[n] //this is dynamic allocation
i mean why are we dynamically allocating memory to it
I just told u that maximum size arrays u can create inside any function is 10^6 when allocating them statically because of limited stack memory
So sometimes this gives segmentation error
To overcome this we allocate space dyanmically i.e from heap .
but still the thing is we have 1<=n<=10^6 … so why dynamically
assume n is 10^6
And u declare two arrays
int arr1[1000000]
int arr2[1000000] but here memory in stack was not available hence it will give segmentation error. thats why dynamically
code is giving runtime error …
Please share the code again