c++ - 不断出现运行时错误 (SEG) 错误消息

标签 c++

我向网站提交了我的问题解决方案(http://opc.iarcs.org.in/index.php/problems/WORDLIST)。它由在线法官提供服务。每次我提交它时,它都会说运行时错误并显示以下消息:

运行时错误:SEG 描述:段错误 运行时间:0

这是什么故障,我该如何解决? 注意:我对 C++ 的了解是中级的,如果你能用简单的方式解释一下,我将不胜感激 这是我的问题代码:

#include<iostream>
#include<ctype.h>
#include<string.h>
using namespace std;
class problem
{
public:
int nol,k,j,w,cp;
char inp[1000][80],qed[1000][80];

problem()
{
k=j=w=cp=0;
}

void input() 
{
    cin>>nol;cin.get();
    for(int i=0;i<nol;i+=1)
    {   
    cin.getline(inp[i],80);
    cin.clear();
    }
    cout<<endl;
    lineprocess();
}

void lineprocess() 
{
    if(k<nol)
    wordprocess();
    else
    display();
}

void wordprocess()
{
    for(;;)
    {
        if(ch(inp[k][cp]))
        {qed[w][j]=0;break;}

        else 
        {
        qed[w][j]=tolower(inp[k][cp]);
        j+=1;cp+=1;
        }
    }
    decide();
}

void decide()
{
    if(inp[k][cp]==0)
    {
        w+=1;
        cp=0;
        j=0;
        k+=1;
        lineprocess();
    }

    else
    {
        w+=1;
        j=0;
        cp+=1;
        wordprocess();
    }
}

int ch(char a)
{
    if(!isalpha(a))
    return 1;
    else return 0;
}

void display()
{
    char dumm[80]="";
    qed[w][0]='\0';
    sortarray();
    removerep();
    for(int i=0;i<=w;i+=1)
    if(strcmp(qed[i],dumm)!=0)
    if(strcmp(qed[i],qed[i+1])!=0)
    cout<<qed[i]<<endl;
}

void sortarray()
{
    char ub[80];
     for(;checksort();)
     {
        for(int q=0;q<w;q++)
        {
            if(strcmp(qed[q],qed[q+1])>0)
            {   
                strcpy(ub,qed[q]);
                strcpy(qed[q],qed[q+1]);
                strcpy(qed[q+1],ub);
            }
        }
     }

}


int checksort()
{
    int t=0;
    for(int i=0;i<w;i+=1)
    {
        if(strcmp(qed[i],qed[i+1])>0)
        {t+=1;break;}
    }
    return t;
}

void removerep()
{
    int nodw=0;
    for(int i=0;i<w;i+=1)
    if(strcmp(qed[i],qed[i+1])!=0)
    nodw+=1;
    cout<<nodw<<endl;
}
};

int main()
{
problem r2d2;
r2d2.input();
return 0;
}

最佳答案

来自链接页面:

You may assume that N ≤ 10000

因为你正在使用

char inp[1000][80],qed[1000][80];

您很可能正在访问超出有效范围的内存,这会给您带来麻烦。将它们更改为:

char inp[10000][80],qed[10000][80];

关于c++ - 不断出现运行时错误 (SEG) 错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33742865/

相关文章:

c++ - 如何在 C++ 的外部类构造函数中初始化嵌套类成员

c++ - c4-pedestrian-detector.cpp:283:8:错误:未在此范围内声明 ‘Show_Detection_Steps’

c++ - 遍历 std::variant 的映射

c++ - 用给定面值的最少硬币数量来定额。贪婪的问题

C++ - 将 JSON 或数组从中转换为 vector

c++ - SFINAE:检测成员变量的存在在 g++ 上不起作用

c++ - 在测试中使用友元

c++ - 通过 Visual C++ (API) 访问 Powershell

c++ - 如何在 boost::python 中向模块添加属性?

c++ - 如何用具体类之一实例化抽象类?