【问题标题】:A sigsev error (runtime) that I can't seem to understand我似乎无法理解的 sigsegv 错误(运行时)
【发布时间】:2017-07-19 07:15:52
【问题描述】:

因此,我在在线法官上提交了此代码,而代码在我的系统上运行良好,但在线法官上的相同输入却给了我这个错误。

    GDB trace:
Reading symbols from solution...done.
[New LWP 4622]
Core was generated by `solution'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  std::_Vector_base<int, std::allocator<int> >::_Vector_impl::_Vector_impl (
    this=0x7ffe815275a0) at /usr/include/c++/7/bits/stl_vector.h:89
89      : _Tp_alloc_type(), _M_start(), _M_finish(), _M_end_of_storage()
#0  std::_Vector_base<int, std::allocator<int> >::_Vector_impl::_Vector_impl (
    this=0x7ffe815275a0) at /usr/include/c++/7/bits/stl_vector.h:89
#1  std::_Vector_base<int, std::allocator<int> >::_Vector_base (
    this=0x7ffe815275a0) at /usr/include/c++/7/bits/stl_vector.h:127
#2  std::vector<int, std::allocator<int> >::vector (this=0x7ffe815275a0)
    at /usr/include/c++/7/bits/stl_vector.h:263
#3  main () at solution.cc:8

由于比赛正在进行,我不确定是否要分享我的代码,但如果有人对此错误有所了解,那将非常有帮助。

请注意,该错误是由 Cpp 14 上完全相同的输入引起的。

好的,所以我决定分享我的代码:

code

#include <bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin>>n;
    int a[n],b[n];
    int max=1000000;
    vector<int> c1[max+1],c2[max+1];
    for(int i=0;i<n;i++){
        cin>>a[i];
        c1[a[i]].push_back(a[i]);
    }
    for(int i=0;i<n;i++){
        cin>>b[i];
        c2[b[i]].push_back(b[i]);
    }
    for(int i=max;i>=1;i--){
        //cout<<i<<endl;
        int f1=0,f2=0;
        for(int j=i;j<=max;j+=i){
            if(c1[j].size()>0){
                f1=1;
                break;
            }
        }
        for(int j=i;j<=max;j+=i){
            if(c2[j].size()>0){
                f2=1;
                break;
            }
        }
        if(f1 && f2){
            int max1=0,max2=0;
            for(int j=i;j<=max;j+=i){
                if(c1[j].size()){
                    for(int k=0;k<c1[j].size();k++){
                        if(max1<c1[j][k])
                            max1=c1[j][k];
                    }
                }
            }
            for(int j=i;j<=max;j+=i){
                if(c2[j].size()){
                    for(int k=0;k<c2[j].size();k++){
                        if(max2<c2[j][k])
                            max2=c2[j][k];
                    }
                }
            }
            cout<<max1+max2;
            return 0;
        }
    }
}

既然这被否决了,我只想知道为什么?我怎样才能更好地提出这个问题?

【问题讨论】:

  • 这个“在线判断”会生成可下载的调试符号和核心转储吗?
  • @zv_ 没有生成调试符号。
  • 这将有助于发布您的相关代码,甚至可以进行更改以找出问题所在。
  • @Maikel 分享了它
  • vector&lt;int&gt; c1[max+1],c2[max+1]; 显然是错误的,并且不是您想要的。请改用vector&lt;int&gt; c1(max+1); vector&lt;int&gt; c2(max+1);

标签: c++ runtime-error c++14


【解决方案1】:

我怀疑您的堆栈空间不足 - 坚持使用 vector
(可变长度数组是非标准的。不要使用它们。)

更改声明就足够了:

vector<int> a(n),b(n);
int max=1000000;
vector<vector<int>> c1(max+1), c2(max+1);

【讨论】:

    【解决方案2】:

    如果你想声明一个向量数组,我建议使用数组容器类。您唯一需要记住的是 max 必须是 const 才能工作。

    const int max = 1000000;
    std::array<std::vector<int, max + 1> c1, c2;
    

    在任何情况下,如果数组的大小在编译时已知,我更喜欢这种解决方案,而不是“经典”向量,因为它在 IMO 上更容易看。

    【讨论】:

    • 不幸的是,std::array 占用的空间与常规数组一样多,因此您对堆栈溢出同样敏感。
    【解决方案3】:

    堆栈跟踪将您的代码留在第 8 行,因此这是第一个查看的位置:

    vector<int> c1[max+1],c2[max+1];
    

    鉴于 max 是 1000000,您正尝试在本地存储中创建 200 万和两个向量。这并不奇怪 - 大多数实现使用堆栈进行本地存储,并具有合理的最大大小。您需要使用new 或(更好)std::make_unique() 创建这些。

    至于#include &lt;bits/stdc++.h&gt;using namespace std;——你真的很想改掉那些坏习惯,它们一起使用特别有害。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多