-1

In the time of code submission I got RE(SIGSEGV) error. My code is:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int T,val,v;
    int min = INT_MAX;
    int N,M;
    cin >> T;
    while(T--){
        cin >> N >> M;
        vector<int> vec(M+1,0);
        vector<int> arr1;
        for(int i=0;i<N;i++){
            cin >> v;
            arr1.push_back(v);
        }
        for(int j=0;j<N;j++){
            cin >> val;
            vec[arr1[j]]+=val;
        }
        for(int i=1;i<=M;i++){
            if((vec[arr1[i]])<min && (vec[arr1[i]])!=0){
                min = vec[arr1[i]];
            }
        }
        cout << " box: "<<endl;
        for(int a=0;a<vec.size();a++){
            cout << vec[a] << " ";
        }
        cout << endl;
        cout << min << endl;
        vec.clear();
        arr1.clear();
    }   
    return 0;
}

Problem link: https://www.codechef.com/MARCH20B/problems/CHPINTU . Can anyone tell me why is this happening? How can I overcome this problem? Thank you.

  • 1
    https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h – Yunnosch Mar 09 '20 at 06:05
  • 1
    You're reading in values from the user, and then using those values as indices into another vector without any checks. A recipe that virtually guarantees running out of bounds in accessing elements of a vector/array. – Peter Mar 09 '20 at 06:11
  • Did you try to debug your code? – DYZ Mar 09 '20 at 06:11
  • Might be useful to also mention the problem that you're trying to solve with this code. – dragosht Mar 09 '20 at 06:22
  • All user inputs are in INTEGER. It only shows error me for large inputted value. @Peter – Rida Farhan Mar 09 '20 at 06:27
  • I've just mentioned the problem. Check it out- https://www.codechef.com/MARCH20B/problems/CHPINTU @dragosht – Rida Farhan Mar 09 '20 at 06:36
  • Please provide a [mre], what is the failing input and what is the code supposed to do? – Alan Birtles Mar 09 '20 at 06:39
  • @RidaFarhan - That's my point. If you use an integer as an index to access elements of a vector or array, that integer needs to be non-negative and less than the number of elements in that vector/array. Your code uses input integers as indices to elements of an array, with no check they are in range. Arrays and vectors don't magically grow if you use an invalid index.For example, one loop reads elements into array `arr1` and the next loop uses elements of `arr1` as indices into `vec` - as in `vec[arr1[j]]+=val;`. If any of the `arr1[j]` are `vec.size()` or more, the behaviour is undefined. – Peter Mar 09 '20 at 06:51
  • I understood. But In question they also clearly mentioned that the inputted value will be in range for sure. If it is possible you should check the problem - https://www.codechef.com/MARCH20B/problems/CHPINTU. @Peter – Rida Farhan Mar 09 '20 at 06:57
  • 1
    If `arr1[i]` is the type of fruit in basket `i`, and `vec[i]` is the price of basket `i`, what is `vec[arr1[i]]`? (You need to practice variable naming.) – molbdnilo Mar 09 '20 at 08:31

1 Answers1

1

N could be smaller than M, in that case this could lead to segmentation errors:

for(int i=1;i<=M;i++){
    if((vec[arr1[i]])<min && (vec[arr1[i]])!=0){
        min = vec[arr1[i]];
    }
}

You should be instead looking over vec[i] instead of vec[arr1[i]]

Nit: this would still give you WA(Wrong Answer) but will not result in SIGSEGV, one of the errors I can find is only initializing min at start, instead of initializing it with INT_MAX in the loop over T

Abhishek Jaisingh
  • 1,614
  • 1
  • 13
  • 23