I was trying to solve a HackerRank question named "Array Manipulation"The link of the problem. I wrote this code below. It works fine. However, when I use an array instead of vector I got segmentation fault at case 7-13. Why did it happen?
long arrayManipulation(int n, vector<vector<int>> queries) {
    vector<long> arr(n);
    long sum=0, max=LONG_MIN;
    for(int i=0;i<n;i++){
        arr[i] = 0;
    }
    for(int i=0;i<queries.size();i++){
        arr[queries[i][0]-1] += queries[i][2];
        if(queries[i][1]<n) arr[queries[i][1]] -= queries[i][2];
    }
    for(int i=0;i<n;i++){
        sum += arr[i];
        if(sum>max) max=sum;
    }
    return max; }
long arrayManipulation(int n, vector<vector<int>> queries) {
    long arr[n];
    long sum=0, max=LONG_MIN;
    for(int i=0;i<n;i++){
        arr[i]=0;
    }
    for(int i=0;i<queries.size();i++){
        arr[queries[i][0]-1]+=queries[i][2];
        if(queries[i][1]<n) arr[queries[i][1]]-=queries[i][2];
    }
    for(int i=0;i<n;i++){
        sum+=arr[i];
        if(sum>max) max=sum;
    }
    return max;
}



queries?long arr[n]wherenis a variable (or function argument) is a VLA (variable length array) which is not valid C++. Some compilers support VLAs as a non-standard extension. Those that do support VLAs typically use process stack space, which is subject to a fairly small quota under most unix environments so the possible size of such an array is therefore significantly smaller than is possible with astd::vector, which uses dynamic memory allocation. A fairly common symptom of exceeding available stack space is a segmentation faultlong arr[n];-- I wish that these coding sites such as HackerRank would turn on the correct compiler options to make this illegal, or at least give that option to do so. This is not legal C++. As to usingstd::vector, use theat()function instead of[ ]to access the elements in the vector. If you now get astd::out_of_rangeexception thrown, then indeed you are going out-of-bounds.