Can't figure out why this program works with a pointer array but not a normal array with large data.

I was trying to solve a problem on hackerrank and I finally got it after a couple of hints from the discussion but I am not sure about a couple of things and why it does/doesn't work.

This is the question: https://www.hackerrank.com/challenges/crush/problem

This is my code where it solves the problem and passes all 16 cases.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Complete the arrayManipulation function below.
long arrayManipulation(int n, vector<vector<int>> queries) {
    long int *index = new long int[n+1] ();
    long int max = 0;
    long int sum = 0;
    long int start, end, amount;
    for(long i=0; i<queries.size(); ++i){
            start = queries[i][0];
            end = queries[i][1];
            amount = queries[i][2];
            index[start] += amount;
            if(end+1 <= n){
                index[end+1] -= amount;
            }
    }
    for(long i=0; i<=n; ++i){
        sum += index[i];
        if(sum > max){
            max = sum;
        }
    }
    delete index;
    return max;
}


Originally I was using a normal array instead of a pointer array and I was failing 7 of the cases with a 'segmentation fault', these were the cases with large amounts of data. I believe segmentation fault is an error in C++ where you try to access memory that you don't have access to but I can't figure out why I get that error when using a normal array when the size of the arrays are the same?

If I replace long int *index = new long int[n+1] (); with long int index[n+1] = {}; and remove the line that says delete index 7 of the 16 test cases give me segmentation fault, I don't understand why?
Last edited on
Well unless n is a compile time constant the second snippet shouldn't even compile since C++ doesn't support Variable Length Arrays. Also using "regular" arrays it is easy to exhaust stack space with large arrays.

Have you considered using std::vector instead of arrays and dynamic memory?


@jlb, just tried it with a vector and it works fine. I am just used to using a fixed array or dynamic memory whenever I am 100% sure that the size is never going to change and if I don't need any of the vectors' functions.

I'm not sure what you mean by "the second snippet shouldn't even compile since C++ doesn't support Variable Length Arrays"?
Creating an array while using a variable to set the size shouldn't be a problem in C++ unless I am misunderstanding your statement?

I'm not sure what you mean by "the second snippet shouldn't even compile since C++ doesn't support Variable Length Arrays"?


Exactly as I said, if n is not a compile time constant that snippet should fail to compile with a strictly conforming C++ compiler. VLA are not allowed in C++, that is a C99 "feature".

Creating an array while using a variable to set the size shouldn't be a problem in C++ unless I am misunderstanding your statement?

Creating an array using a "variable" to set the size is forbidden with a strictly conforming C++ compiler. The C++ standard requires a compile time constant for the size of an array, not a variable.

I am just used to using a fixed array or dynamic memory whenever I am 100% sure that the size is never going to change and if I don't need any of the vectors' functions.

Even if you know that the size is never going to change I recommend std::vector over "raw" arrays. You can initialize the vector to a specific size and then use it just like a "raw" array, with the benefit that the vector always knows it's size unlike "raw" arrays.
Topic archived. No new replies allowed.