You are viewing a single comment's thread. Return to all comments →
instead of writing long int *a=long int[n+1]
if I write long int a[n+1];
It shows segmentation fault after test case 7. why?
yes ,the same with you, i am confused
you aren't allocating space for a that way. He uses new to create an array on the heap. You are trying to declare it on the stack, but you can't do that with a dynamic value like 'n'
As per my knowledge, u can create an array to size upto 10^6 in stack and for size above it we need to create in heap so we do it dynamically.
for me the best explanation for the dynamic allocation was:-here!
If you have a problem understanding dynamic memory then make use of vector . It allocates memory dynamically wherein the user doesn't have to deal with pointers .
vector arr(n); declares an array of long int of length n.