[英]How can I get rid of segmentation error?
My goal is to find the maximum value of (minimum of p consecutive number)*p. 我的目标是找到(p个连续数的最小值)* p的最大值。 Here 1<=p<=N and 1<=N<=100000 and 1<=number<=1000000.I have partially solved the problem.But for some cases I am getting this runtime error. 这里1 <= p <= N和1 <= N <= 100000和1 <= number <= 1000000。我已经部分解决了这个问题,但是在某些情况下,我遇到了运行时错误。
" terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information." “抛出'std :: bad_alloc'实例之后调用终止命令what():std :: bad_alloc该应用程序已请求运行时以一种不寻常的方式终止它。请联系应用程序的支持团队以获取更多信息。”
I have found that the error is because program is trying to access memory that is not allowed. 我发现该错误是因为程序试图访问不允许的内存。 But I am not getting which part of code is trying to access unallocated memory.Please help me. 但是我没有得到哪一部分代码试图访问未分配的内存,请帮帮我。
#include <iostream>
using namespace std;
int main(){
int N=0;
cin>>N;
int **A,*B,i,j,num;
A = new int*[N];
for(i=0;i<N;i++)
A[i] = new int[N]();
B = new int[N]();
for(i=0;i<N;i++){
cin>>num;
A[0][B[0]] = num;
for(j=0;j<B[0];j++){
A[j+1][B[j+1]] = min(A[j][B[j+1]],A[j][B[j+1]+1]);
B[j+1]++;
}
B[0]++;
}
long long maxim = 0;
for(i=0;i<N;i++){
for(j=0;j<B[i];j++){
if(A[i][j]*(i+1) > maxim)
maxim = A[i][j]*(i+1);
}
}
cout<<maxim;
delete []A;
delete []B;
return 0;
}
std::bad_alloc is thrown when failing to allocate memory . 无法分配内存时,将抛出std :: bad_alloc。
You are trying to allocate space for 100000 * 100000 integers. 您正在尝试为100000 * 100000整数分配空间。 That's ~40GB of RAM, which you probably do not have. 那是大约40GB的RAM,您可能没有。
Part of becoming a software engineer is learning to break problems into smaller chunks. 成为软件工程师的一部分是学习将问题分解为小块。 To solve this issue you should start by breaking the problem down to the smallest unit. 要解决此问题,您应该首先将问题分解为最小的单元。
int main()
{
size_t dimension = 100000;
int** outer = new int[dimension];
for (size_t i = 0; i < dimension; ++i) {
outer[i] = new int[dimension];
}
}
Compile and load into a debugger, step through and find where the exception occurs. 编译并加载到调试器中,逐步查找异常发生的位置。 Or you could write thusly: 或者您可以这样写:
#include <iostream>
int main()
{
size_t dimension = 100000;
size_t allocated = 0;
int** outer = new int*[dimension];
allocated += sizeof(int*) * dimension;
for (size_t i = 0; i < dimension; ++i) {
try {
outer[i] = new int[dimension];
allocated += sizeof(int) * dimension;
} catch (std::bad_alloc&) {
std::cerr << "bad alloc at iteration " << i << " with " << allocated << " bytes allocated\n";
return -1;
}
}
}
Or 要么
#include <iostream>
int main()
{
size_t dimension = 100000;
std::cout << "Allocating outer: " << (dimension * sizeof(int*)) << " bytes\n";
int** outer = new int[dimension];
for (size_t i = 0; i < dimension; ++i) {
std::cout << "Allocating outer[" << i << "] (" << (dimension * sizeof(int)) << " bytes\n";
outer[i] = new int[dimension];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.