Code gets SIGSEGV when using Dynamic Memory

My Code : Solution: 67508149 | CodeChef
Problem Statement : Contest Page | CodeChef

If I initialize my array like this, I get SIGSEGV

int *arr = (int*)calloc(N+1, sizeof(int));

and if I declare my array this, I get AC while submission

int arr[N+1];

Can someone help me understand this weird behaviour ?

calloc does set the allocated memory to 0.

For example, can you ever get a case for do { i++; } while(arr[i]<=pivot) where i goes over n? arr[n] is 0 in the calloc case, given that you deal with positive numbers, 0 will be smaller than any pivot.

global variables and static variables are automatically initialized to zero. If you had int arr[N+1] inside the main function, then it will not be initialized to 0.

2 Likes

Ohh I never thought of this, now I got it Thanks :slightly_smiling_face: