简体   繁体   English

在 C++ 代码中出现分段错误错误

[英]Getting Segmentation Fault error in C++ code

#include<bits/stdc++.h>
using namespace std;
//FIRST REPEATING ELEMENT (APPROACH 2)

int main()
{
    cout<<"running";
    int n;
    cin>>n;
    int arr[n];
    for(int i=1; i<=n; i++)
        cin>>arr[i];

    const int N = 1e5+2;
    int idx[N];

    for(int i=0;i<N;i++)
        idx[i] = -1;

    int minidx = INT_MAX;

    for(int i=0;i<n;i++)
    {
        if(idx[arr[i]] != -1)
            minidx = min(minidx, idx[arr[i]]);
        else
            idx[arr[i]] = i;
    }

    if(minidx == INT_MAX)
        cout<<"-1"<<endl;
    else
        cout<<minidx + 1<<endl;

    return 0;

}

I am writing this code for "First Repeating Element" question and trying to get the index of the first element that repeats.我正在为“第一个重复元素”问题编写此代码,并尝试获取第一个重复元素的索引。 On debugging, I am getting segmentation fault.在调试时,我遇到了分段错误。 int main() { (in this line) int main() { (在这一行)

What does it mean and what can I do to remove this.这是什么意思,我该怎么做才能删除它。

const int N = 1e5+2;
int idx[N];

That sounds like a big array to be allocated on the stack (400kb!).这听起来像是要在堆栈上分配的大数组(400kb!)。 Try using malloc() to allocate an array of that size.尝试使用 malloc() 分配该大小的数组。 Change that to something like将其更改为类似

const int N = 1e5+2;
int* idx = (int*)malloc(N * sizeof(int));

You should also do the same for arr , particularly if you use large values for n.您也应该对arr执行相同的操作,尤其是当您使用较大的 n 值时。

The program has undefined behavior because you're going out of bound of the array.该程序具有未定义的行为,因为您超出了数组的范围。 This is because in your for loop condition, you're using i<=n instead of i<n .这是因为在您的for循环条件中,您使用的是i<=n而不是i<n

Undefined behavior means anything 1 can happen including but not limited to the program giving your expected output.未定义的行为意味着任何事情1都可能发生,包括但不限于给出预期输出的程序。 But never rely (or make conclusions based) on the output of a program that has undefined behavior.永远不要依赖(或基于)具有未定义行为的程序的输出。 The program may just crash.该程序可能会崩溃。

So the output that you're seeing(maybe seeing) is a result of undefined behavior.所以你看到的输出(也许看到)是未定义行为的结果。 And as i said don't rely on the output of a program that has UB.正如我所说,不要依赖具有 UB 的程序的输出 The program may just crash.该程序可能会崩溃。

So the first step to make the program correct would be to remove UB.因此,使程序正确的第一步是删除 UB。 Then and only then you can start reasoning about the output of the program.只有这样,您才能开始推理程序的输出。

Additionally, in Standard C++ the size of an array must be a compile time constant.此外,在标准 C++ 中,数组的大小必须是编译时间常数。 This means that the following is incorrect in your program.这意味着您的程序中的以下内容不正确。

int n;
cin >> n;
int arr[n]; //not standard C++ since n is not a constant expression

1 For a more technically accurate definition of undefined behavior see this where it is mentioned that: there are no restrictions on the behavior of the program . 1有关未定义行为的更技术上准确的定义,请参见此处提到:对程序的行为没有限制

Segmentation fault is due to usage of memory that you don't own, or in other words stack over flow.分段错误是由于使用了您不拥有的内存,或者换句话说是堆栈溢出。 Most likely because you're creating an array of size 1e5+2 and looping over it, initializing every element.很可能是因为您正在创建一个大小为 1e5+2 的数组并对其进行循环,初始化每个元素。 That's not the best way to find the recurrence of an element.这不是找到元素重复的最佳方法。 Instead use a map with key value pairs.而是使用带有键值对的映射。

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

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