C++ 运行时错误(SIGABRT)
我需要你的帮助,关于这个代码;我得到运行时错误,尽管有正确的代码。我知道SIGABRT错误是由于内存过多造成的;你们能告诉我我的代码哪里出错了,怎么改正吗C++ 运行时错误(SIGABRT),c++,runtime,C++,Runtime,我需要你的帮助,关于这个代码;我得到运行时错误,尽管有正确的代码。我知道SIGABRT错误是由于内存过多造成的;你们能告诉我我的代码哪里出错了,怎么改正吗 #include<iostream> #include<stdlib.h> #include<string> int count=0; int a[1000]; using namespace std; void check(string *str,int p
#include<iostream>
#include<stdlib.h>
#include<string>
int count=0;
int a[1000];
using namespace std;
void check(string *str,int p)
{
int j=0,i,k;
int flag=0;
char *first=new char[p];
char *last=new char[p];
for(int i=0;i<p;i++)
{
first[j]=str[i].front();
last[j]=str[i].back();
j++;
}
for(i=0;i<j;i++)
for(k=0;k<j;k++)
{
if(flag>=2)
{
a[count++]=0;
delete first;
delete last;
return;
}
if(first[i]==last[k])
break;
if(k==j-1)
flag++;
}
if(flag>=2)
{
a[count++]=0;
delete first;
delete last;
return;
}
if(i==j)
a[count++]=1;
delete first;
delete last;
}
int main()
{
int t,p;
cin>>t;
for(int i=0;i<t;i++)
{
cin>>p;
string *str=new string[p];
for(int i=0;i<p;i++)
cin>>str[i];
check(str,p);
delete str;
}
for(int i=0;i<count;i++)
if(a[i]==0)
cout<<"\nThe door cannot be opened.";
else
cout<<"\nOrdering is possible.";
return 0;
}
#包括
#包括
#包括
整数计数=0;
INTA[1000];
使用名称空间std;
无效检查(字符串*str,int p)
{
int j=0,i,k;
int标志=0;
char*first=新字符[p];
char*last=新字符[p];
对于(int i=0;i>t;
对于(inti=0;i>p;
string*str=新字符串[p];
对于(int i=0;i>str[i];
检查(str,p);
删除str;
}
对于(int i=0;i我相信您的代码中有一个主要问题。您分配数组但删除单指针。对于new[]
您应该使用delete[]
而不是delete
。我相信它应该解决错误。SIGABRT并不仅仅意味着“内存使用过度”。您的代码相当复杂,而且绝对没有迹象表明您用于重现问题的输入字符串是什么[这可能会影响程序的结果!]代码的解决方案是将作为输入的字符串的最后一个字符和第一个字符分开。然后取消第一个和最后一个数组中的所有公共字符,如果没有公共字符,则增加标志,如果标志大于2,则返回标志。我试着用动态分配只是分配str[100]和first[100]等等,但我得到了错误,它给出了sigabrt sigsev。你能告诉我你在哪一行代码中得到了这个错误吗?这肯定会有帮助问题解决了我使用了delete[]释放内存。谢谢你的帮助。这段代码与codechef中的一个问题有关。这些家伙不提供测试用例,也不提供代码的错误说明。调试这些问题很痛苦。你知道我可以在哪个网站上练习使用正确的测试用例正确编码。再次感谢!!