簡體   English   中英

無法解決分段錯誤

[英]Unable to resolve segmentation fault

您好,我是新手,在運行程序時出現分段錯誤,但無法找到其背后的原因。 我用谷歌搜索並知道它是由於 memory 違規而發生的,但無法在我的代碼中找到導致 seg 錯誤的原因。

下面的代碼

#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;
}

我使用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)

#5表示16.9行中的問題是排序 function但無法在此處找到導致 seg 錯誤的原因

感謝您的幫助和時間。

問題很可能是這兩行:

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

首先,您定義變量n ,該變量未初始化並且將具有不確定的值。

然后使用n的不確定值定義數組arr 在將n初始化為特定值之前,您無法執行此操作。 在此處使用n會導致未定義的行為

第二行實際上還有另一個問題,因為它是一個可變長度數組,而 C++ 並沒有這些。 改用std::vector

為了解決這兩個問題,你應該有這樣的東西:

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

    // ...
}

從更個人的角度來看,該代碼看起來像是為在線“競賽”或評委網站制作的。 這些網站不是教學或學習資源,您不應該將它們用作學習編程語言或一般編程的一種方式。 請獲取一些好的 C++ 書籍並參加一些課程以正確學習。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM