简体   繁体   English

无法解决分段错误

[英]Unable to resolve segmentation fault

Hello am newbie here am getting segmentation fault when running the program but unable to find reason behind it.您好,我是新手,在运行程序时出现分段错误,但无法找到其背后的原因。 I have googled and know that it occurs due to memory violation but unable to find what is causing seg fault in my code.我用谷歌搜索并知道它是由于 memory 违规而发生的,但无法在我的代码中找到导致 seg 错误的原因。

Code below下面的代码

#include <iostream>
#include<algorithm>
using namespace std;
int main()
{
 int t, n, m;
    int  arr[n];
    cin >> t;
    while (t--)
    {
        cin >> n >> m;
        for (int i = 0; i < n; i++)
        {
            cin >> arr[i];
        }
        sort(arr, arr + n);
        int distinct = 1;
        for (int i = 1; i < n; i++)
        {
            if (arr[i] != arr[i - 1])
            {
                distinct++;
            }
        }
        if (distinct < m)
        {
            cout << "YES" << endl;
        }
        else
        {
            cout << "NO" << endl;
        }
    }

    return 0;
}

I used lldb for debugging and got results as follows:我使用lldb进行调试,得到的结果如下:

divyangbhamat@Divyangs-MacBook-Air CodeChef % lldb ./a.out
(lldb) target create "./a.out"
Current executable set to '/Users/divyangbhamat/Documents/C++/CodeChef/a.out' (x86_64).
(lldb) run
Process 1316 launched: '/Users/divyangbhamat/Documents/C++/CodeChef/a.out' (x86_64)
1
5
6
1
2
1
5
3
Process 1316 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x7ffe00000007)
    frame #0: 0x00007fff2029b328 libc++.1.dylib`unsigned int std::__1::__sort3<std::__1::__less<int, int>&, int*>(int*, int*, int*, std::__1::__less<int, int>&) + 4
libc++.1.dylib`std::__1::__sort3<std::__1::__less<int, int>&, int*>:
->  0x7fff2029b328 <+4>:  movl   (%rsi), %ecx
    0x7fff2029b32a <+6>:  movl   (%rdi), %r8d
    0x7fff2029b32d <+9>:  movl   (%rdx), %r9d
    0x7fff2029b330 <+12>: cmpl   %r8d, %ecx
Target 0: (a.out) stopped.
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x7ffe00000007)
  * frame #0: 0x00007fff2029b328 libc++.1.dylib`unsigned int std::__1::__sort3<std::__1::__less<int, int>&, int*>(int*, int*, int*, std::__1::__less<int, int>&) + 4
    frame #1: 0x00007fff2029b3a2 libc++.1.dylib`unsigned int std::__1::__sort4<std::__1::__less<int, int>&, int*>(int*, int*, int*, int*, std::__1::__less<int, int>&) + 31
    frame #2: 0x00007fff2029b40a libc++.1.dylib`unsigned int std::__1::__sort5<std::__1::__less<int, int>&, int*>(int*, int*, int*, int*, int*, std::__1::__less<int, int>&) + 37
    frame #3: 0x0000000100003101 a.out`void std::__1::sort<int*, std::__1::__less<int, int> >(__first=0x00007ffe00000003, __last=0x00007ffe00000017, __comp=__less<int, int> @ 0x00007ffeefbff6f8) at algorithm:4125:5
    frame #4: 0x0000000100002ffd a.out`void std::__1::sort<int*>(__first=0x00007ffe00000003, __last=0x00007ffe00000017) at algorithm:4133:5
    frame #5: 0x0000000100002ef5 a.out`main at nobelPrize.cpp:16:9
    frame #6: 0x00007fff20353621 libdyld.dylib`start + 1
    frame #7: 0x00007fff20353621 libdyld.dylib`start + 1
(lldb)

Frame #5 says problem in 16.9 line that is the sort function but unable to find why it is causing seg fault here#5表示16.9行中的问题是排序 function但无法在此处找到导致 seg 错误的原因

Thank you for your help and time.感谢您的帮助和时间。

The problem is very likely these two lines:问题很可能是这两行:

int t, n, m;
int  arr[n];

First you define the variable n , which is uninitialized and will have an indeterminate value.首先,您定义变量n ,该变量未初始化并且将具有不确定的值。

Then you define the array arr using the indeterminate value of n .然后使用n的不确定值定义数组arr You can't do this until you have initialized n to a specific value.在将n初始化为特定值之前,您无法执行此操作。 Using n here leads to undefined behavior .在此处使用n会导致未定义的行为

The second line have actually another problem, because it's a variable-length array , and C++ doesn't really have those.第二行实际上还有另一个问题,因为它是一个可变长度数组,而 C++ 并没有这些。 Use std::vector instead.改用std::vector

To solve both issues, you should have something like this instead:为了解决这两个问题,你应该有这样的东西:

while (t--)
{
    int n, m;
    cin >> n >> m;
    std::vector<int> arr(n);  // Create a vector of n elements

    // ...
}

On a more personal note the code looks like it was made for an online "competition" or judge site.从更个人的角度来看,该代码看起来像是为在线“竞赛”或评委网站制作的。 Such sites are not teaching or learning resources, you should never use them as a way to learn programming languages or programming in general.这些网站不是教学或学习资源,您不应该将它们用作学习编程语言或一般编程的一种方式。 Please get some good C++ books and take some classes to learn properly.请获取一些好的 C++ 书籍并参加一些课程以正确学习。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM