SIGSEGV,分段错误。 #0 __memmove_avx_unaligned_erms()on HackerRank

问题描述 投票:-1回答:1

有一个黑客等级Question

我正在使用C ++,我试图解决它,当我在hackerrank上运行它时,我的代码似乎可以在我的计算机上正常工作错误

[New LWP 17077]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `./Solution'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  __memmove_avx_unaligned_erms ()
    at ../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:498
To enable execution of this file add
    add-auto-load-safe-path /usr/local/lib64/libstdc++.so.6.0.25-gdb.py
line to your configuration file "//.gdbinit".
To completely disable this security protection add
    set auto-load safe-path /
line to your configuration file "//.gdbinit".
For more information about this security protection see the
"Auto-loading safe path" section in the GDB manual.  E.g., run from the shell:
    info "(gdb)Auto-loading safe path"

我的代码

vector<int> cutTheSticks(vector<int> arr) {
vector<int> result;
sort( arr.begin(), arr.end() );
    while( arr.size() > 0 ) {
        result.push_back( arr.size() );
        int min = arr[0];
        for( int i = 0; i < arr.size(); i++ ) {
            while( arr[i] == min ) {
                //cout<<"deleting index : "<<i<<"  element : "<<arr[i]<<endl;
                arr.erase( arr.begin() );
            }
            //cout<<"subt at index : "<<i<<"  element : "<<arr[i]<<" - "<<min<<endl;
            arr[i] = arr[i]-min;
        }
    }
    return result;
}

我无法理解这是我的错,还是hackerrank的问题,如果是我的错,我该如何解决。

c++ compiler-warnings competitive-coding
1个回答
0
投票

您正在更改数组的大小。

执行此操作:

 std::cout << "Size: " << arr.size() << "\n";
 std::cout << "i:    " << i << "\n";
 if (i >= arr.size()) {
     std::cout << "Error\n";
     throw "Something went wrong";
 }
 arr[i] = arr[i]-min;

 if (arr.size() == 0) {
     std::cout << "Error\n";
     throw "Something went wrong";
 }
 arr.erase( arr.begin() );
© www.soinside.com 2019 - 2024. All rights reserved.