Home > Net >  why this is giving segmentation fault error?
why this is giving segmentation fault error?

Time:05-25

Why this below code is giving segmentation fault?
The question says
Given an array of integers Arr of size N and a number K. Return the maximum sum of a subarray of size K.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

Constraints:
1<=N<=106
1<=K<=N

// { Driver Code Starts
#include<bits/stdc  .h> 
using namespace std; 

 // } Driver Code Ends
 
    
    
    class Solution{
        
    public:

    int maximumSumSubarray(int K, vector<int>&Arr , int N){
         int M = 1e6 10;
         long long A[M];
    for(int i=0; i<M; i  ){
    A[i]=0;
    }
        
        int sum=0;
        long long arr[M];
        for(int i=0; i<N; i  ){
              A[Arr[i]];
            
        }
        int l=0;
          for(int i=0; i<M; i  ){
              if(A[i]>=1){
                 arr[l]=i;
                 l  ;
              }
          }  
            
            
        for(int j= (N-K); j<l; j  ){
           sum =arr[j];
           
        }
        
        return sum;
    }
};

// { Driver Code Starts.
int main() 
{ 
    int t;
    cin>>t;
    while(t--)
    {
        int N,K;
        cin >> N >> K;;
        vector<int>Arr;
        for(int i=0;i<N;  i){
            int x;
            cin>>x;
            Arr.push_back(x);
        }
        Solution ob;
        cout << ob.maximumSumSubarray(K,Arr,N) << endl;
    }
    return 0; 
}   // } Driver Code Ends

CodePudding user response:

you surely dont mean this

     int M = 1e6 10;
     long long A[M];

thats asking for 8 megs of stack.

use std::vector instead

CodePudding user response:

The array is too large for the stack. You need to allocate the space dynamically. Create a pointer and use the "new" constructor to point to it. Try something like this:

long long  * ABig = new long long  [1e6 10];

You can also check the error log to see what is causing the segmentation fault. For instance, if you compile this:

long long A[1e6 10];

you will get "exception unhandled, stack overflow" error.

CodePudding user response:

Why this below code is giving segmentation fault?

Since the entire purpose of a segmentation fault is to interrupt your program in a way amenable to debugging, I'd usually encourage you to actually try running your code in a debugger before asking a question. You might figure out the answer, and if you don't, at least you'll have some more information to add to the question.

Having said all that, it probably won't help much when you do this:

        long long A[M];
        // ...
        long long arr[M];

because declaring two enormous arrays with automatic local scope isn't going to work. Just use std::vector for them, as you are already for the parameter Arr.

Also, don't declare three variables called Arr, arr and A ... between these variable names and the indentation, your code is virtually unreadable.

Oh, and this line assumes Arr[i] is non-negative:

  A[Arr[i]];

If that's a real requirement, then you should be using something like std::vector<unsigned>. If it's not, you should probably bias your index by std::numeric_limits<int>::min() to get a non-negative index.

  • Related