为什么这会给分段故障错误?

发布于 2025-02-01 01:21:34 字数 1393 浏览 6 评论 0原文

为什么下面的代码给出了细分故障?
问题说
给定一个大小为n和数字K的整数rar。 预期时间复杂性:o(n)
预期辅助空间:o(1)

约束:
1< = n< = 10 6
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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

第七度阳光i 2025-02-08 01:21:34

您肯定不是那样的意思

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

是要求8毫克的堆栈。

使用std :: vector而不是

you surely dont mean this

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

thats asking for 8 megs of stack.

use std::vector instead

睫毛溺水了 2025-02-08 01:21:34

阵列对于堆栈而言太大。您需要动态分配空间。创建指针并使用“新”构造函数指向它。尝试这样的事情:

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

您还可以检查错误日志以查看导致分割故障的原因。例如,如果您对此进行了编译:

long long A[1e6+10];

您将获得“未经手的异常,堆栈溢出”错误。

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.

飘过的浮云 2025-02-08 01:21:34

为什么以下代码给出分段错误?

由于细分故障的全部目的是以可调试的方式中断您的程序,因此我通常鼓励您在提出问题之前实际尝试在调试器中运行代码。您可能会弄清楚答案,如果您不这样做,至少您将还有更多信息可以添加到问题中。

说了这一切,当您这样做时,这可能不会有太大帮助:

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

因为宣布具有自动本地范围的两个巨大阵列无法正常工作。只需为它们使用std :: vector,因为您已经为参数arr使用。

另外,不要声明三个变量arrarra ...在这些变量名称和凹痕之间,您的代码实际上是不可读。

哦,这一行假设arr [i]是非负的:

++A[Arr[i]];

如果这是真正的要求,那么您应该使用std :: vector&lt; unsigned&gt;之类的东西。如果不是这样,您可能应该通过std :: numeric_limits&lt; int&gt; :: min()偏向索引,以获取一个非负索引。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文