C++分段错误问题

发布于 2024-08-04 06:13:54 字数 1901 浏览 5 评论 0原文

我的程序崩溃了,这对我来说似乎很好,当然我的程序却说不然,这让我很困惑。

我目前正在处理的函数的这个片段:

        for(int k = 0; k < dictionary[k].size(); k++)
        {
            //"i" represents the fragment's first character
            //while "k" represents the dictionary first character
            if(fragments[i][j] == dictionary[k][j]) {
                token++;
                cout << token << endl;
            }
        }

可能是问题所在。当我调试问题时,调试器会转到代码片段中的第一行:

    for(int k = 0; k < dictionary[k].size(); k++)

然后当我尝试转到下一行时崩溃。 在调试器中,此窗口在我的代码块中打开:

Signal Received

Program Received Singal SIGEGV, segmentation fault. Do you want to view backtrace?

我单击“是”,这对我来说似乎是任意的。

有谁知道我做错了什么?

如果需要回溯(窗口显示“调用堆栈”),我将在需要时对其进行编辑

编辑:这是整个函数,我认为这不是必需的

void Language::compare()
{
    int para = getParameters(0); //eg. 3

    int valid = para;
    int token = 0;

    for(int i = 0; i < para; i++)
    {
        //If the string is creater than 2 characters
        if(fragments[i].length() > 1) {
            for(int j = 0; j < fragments[i].length(); j++)
            {
                //Checking if that character match in dictionary
                for(int k = 0; k < para; k++) //Changed and now works,
                {
                    //"i" represents the fragment's first character
                    //while "k" represents the dictionary first character
                    if(fragments[i][j] == dictionary[k][j]) { //But now this line crashes
                        token++;
                        cout << token << endl;
                    }
                }
                if(token == 0) {
                    break;
                }
            }
        }
        else {
        //...
        }
    }
}

字典和片段在“语言”类中声明” 两者都是向量。

I've gotten a crash in my program which seems fine to me, of course my program says otherwise, which confuses me.

This snippet of my function I am currently working on:

        for(int k = 0; k < dictionary[k].size(); k++)
        {
            //"i" represents the fragment's first character
            //while "k" represents the dictionary first character
            if(fragments[i][j] == dictionary[k][j]) {
                token++;
                cout << token << endl;
            }
        }

might be the problem. When I debug the problem, the debugger goes to the first line in the snippet:

    for(int k = 0; k < dictionary[k].size(); k++)

then crashes when I try go to the next.
In the debugger, this window opens up in my Code Blocks:

Signal Received

Program Received Singal SIGEGV, segmentation fault. Do you want to view backtrace?

I clicked yes and it just seems arbitrary to me.

Does anyone know what I've done wrong?

If the backtrace is needed(the window says Call Stack), I'll edit it on later if needed

EDIT: This is the whole function, which I've thought it would not be necessary

void Language::compare()
{
    int para = getParameters(0); //eg. 3

    int valid = para;
    int token = 0;

    for(int i = 0; i < para; i++)
    {
        //If the string is creater than 2 characters
        if(fragments[i].length() > 1) {
            for(int j = 0; j < fragments[i].length(); j++)
            {
                //Checking if that character match in dictionary
                for(int k = 0; k < para; k++) //Changed and now works,
                {
                    //"i" represents the fragment's first character
                    //while "k" represents the dictionary first character
                    if(fragments[i][j] == dictionary[k][j]) { //But now this line crashes
                        token++;
                        cout << token << endl;
                    }
                }
                if(token == 0) {
                    break;
                }
            }
        }
        else {
        //...
        }
    }
}

dictionary and fragments is declared in the class "Language" which are both vector.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我不吻晚风 2024-08-11 06:13:54

我怀疑您打算使用 dictionary[k].size() 作为循环控制的一部分,因为循环正在对 k 进行迭代。您可能指的是 dictionary.size()dictionary[i].size() 吗?

I doubt you intended to use dictionary[k].size() as part of your loop control there, since the loop is iterating over k. Did you mean dictionary.size() or dictionary[i].size() perhaps?

绻影浮沉 2024-08-11 06:13:54

这句话:

for(int k = 0; k < dictionary[k].size(); k++)

确实看起来很可疑。您确定不想循环到字典数组/集合的大小吗?

如果您发布了dictionary的定义,它可能会帮助我们给出一些具体的建议。

This line:

for(int k = 0; k < dictionary[k].size(); k++)

does seem suspect. Are you sure you don't want to loop up to the size of the dictionary array/collection?

If you posted the definition of dictionary it might help us to give some concrete suggestions.

此刻的回忆 2024-08-11 06:13:54

片段[i][j] ==字典[k][j]

在尝试取消引用之前,您应该确保 dictionary[k].size()>j 字典[k][j]

fragments[i][j] == dictionary[k][j]

You should make sure dictionary[k].size()>j before you attempt to dereference dictionary[k][j].

美人迟暮 2024-08-11 06:13:54

进入循环时 j 的值是多少?

我同意其他人的观点:

for(int k = 0; k

似乎是假的。 这样的东西

像for(int k = 0; k

似乎更有意义。

What is the value of j on entry to the loop?

And I agree with the other guys:

for(int k = 0; k < dictionary[k].size(); k++)

seems bogus. Something like

for(int k = 0; k < dictionary[m].size(); k++)

would seem to make more sense.

空名 2024-08-11 06:13:54

关于 for 循环中的测试的其他评论看起来很可疑,可能是正确的,但此外,如果您第一次尝试进入循环时遇到此段错误,那么您可能正在使用字典 向量 没有条目。

在这种情况下,dictionary[0].size() 具有未定义的行为(这很可能是段错误)。

The other comments about the test in the for loop looking suspect are probably correct, but in addition if you're getting this segfault the first time you try to go into the loop then it's possible that you're working with a dictionary vector<> that has no entries.

In this case dictionary[0].size() has undefined behavior (which might well be a segfault).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文