my code does not run for the input 1 and 1000 or any other larger inputs(我的代码不适用于输入 1 和 1000 或任何其他更大的输入)
问题描述
当我尝试为输入 1 和 1000 运行此代码时,它会显示分段错误.此代码中的更正是什么?
when i am trying to run this code for input 1 and 1000 it shows me segmentation fault .what will be the correction in this code ?
void sorting(int sum[],long int k);
int main() {
int sum[100000];
int L,R,i,j;
long int k=0;
cin>>L;
cin>>R;
for(i=L;i<=R;i++)
{
for(j=i;j<=R;j++)
{
sum[k]=i^j;
k++;
}
}
sorting(sum,k);
cout<<sum[k-1];
return 0;
}
void sorting(int sum[],long int k)
{
int i,j;
long int temp;
for(i=0;i<k;i++)
{
for(j=0;j<k;j++)
{
if(sum[i]<=sum[j])
{
temp=sum[i];
sum[i]=sum[j];
sum[j]=temp;
}
}
}
}
推荐答案
由于堆栈溢出导致分段错误.这一行:
The segmentation fault is caused by stack overflow. This line:
int sum[100000];
sum
使用了400K的栈空间,比正常的栈空间大.
sum
uses 400K spaces of stack, which is bigger than the normal size of stack.
要解决这个问题,你可以使用 std::vector
来实现 sum
.
To fix the problem, you can use std::vector
to implement sum
instead.
这篇关于我的代码不适用于输入 1 和 1000 或任何其他更大的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我的代码不适用于输入 1 和 1000 或任何其他更大的输入


- C++ 协变模板 2021-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 近似搜索的工作原理 2021-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- 静态初始化顺序失败 2022-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 从python回调到c++的选项 2022-11-16
- STL 中有 dereference_iterator 吗? 2022-01-01