为什么这会给分段故障错误?
为什么下面的代码给出了细分故障?
问题说
给定一个大小为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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您肯定不是那样的意思
是要求8毫克的堆栈。
使用
std :: vector
而不是you surely dont mean this
thats asking for 8 megs of stack.
use
std::vector
instead阵列对于堆栈而言太大。您需要动态分配空间。创建指针并使用“新”构造函数指向它。尝试这样的事情:
您还可以检查错误日志以查看导致分割故障的原因。例如,如果您对此进行了编译:
您将获得“未经手的异常,堆栈溢出”错误。
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:
You can also check the error log to see what is causing the segmentation fault. For instance, if you compile this:
you will get "exception unhandled, stack overflow" error.
由于细分故障的全部目的是以可调试的方式中断您的程序,因此我通常鼓励您在提出问题之前实际尝试在调试器中运行代码。您可能会弄清楚答案,如果您不这样做,至少您将还有更多信息可以添加到问题中。
说了这一切,当您这样做时,这可能不会有太大帮助:
因为宣布具有自动本地范围的两个巨大阵列无法正常工作。只需为它们使用
std :: vector
,因为您已经为参数arr
使用。另外,不要声明三个变量
arr
,arr
和a
...在这些变量名称和凹痕之间,您的代码实际上是不可读。哦,这一行假设
arr [i]
是非负的:如果这是真正的要求,那么您应该使用
std :: vector&lt; unsigned&gt;
之类的东西。如果不是这样,您可能应该通过std :: numeric_limits&lt; int&gt; :: min()
偏向索引,以获取一个非负索引。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:
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 parameterArr
.Also, don't declare three variables called
Arr
,arr
andA
... between these variable names and the indentation, your code is virtually unreadable.Oh, and this line assumes
Arr[i]
is non-negative: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 bystd::numeric_limits<int>::min()
to get a non-negative index.