简体   繁体   中英

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.

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:

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

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.

Then you define the array arr using the indeterminate value of n . You can't do this until you have initialized n to a specific value. Using n here leads to undefined behavior .

The second line have actually another problem, because it's a variable-length array , and C++ doesn't really have those. Use std::vector instead.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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