数组操作中的分段错误

Segmentation fault in array manipulation

我正在解决 hackerrank 问题 https://www.hackerrank.com/challenges/crush/problem。这是代码:

long arrayManipulation(int n, vector<vector<int>> queries) {
     long ans[n] = {0};
     cout << sizeof(ans)/sizeof(long) << endl;

    cout << queries.size() << " "<< n << endl;
    for (long i = 0; i < queries.size(); i++) {
        long s = queries[i][0];
        long e = queries[i][1];
        long k = queries[i][2];
        //cout << s << " start " << e << " end " << endl;
        ans[s-1] += k;
    if (e != n-1) {
        ans[e] -= k; 
    }
        //cout << ans[s-1] << " " << ans[e] << endl;
    } 
    //cout << sizeof(ans)/sizeof(long) << endl;
    /*for (auto i = 0; i < n; i++) {
        cout << ans[i] << " ";
    }
    cout << endl;
    for (long i = 1; i < n; i++) {
        ans[i] += ans[i-1];
    }
    for (auto i = 0; i < n; i++) {
        cout << ans[i] << " ";
    }
    cout << endl;
*/
    sort(ans, ans+n+1);
/*    for (auto i = 0; i < n; i++) {
        cout << ans[i] << " ";
    }*/
   
    return ans[n-1];
}

问题是它在少量输入时运行良好,但当输入约为 1000000 时,它会出现分段错误,并且当我全局声明 ans 数组且固定大小为 10000000 时,它会在大量输入时运行。这是我使用以上代码 运行 编程时 valgrind 的输出:

    ❯ less ip.txt | valgrind ./am==7397== Memcheck, a memory error detector
==7397== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==7397== Using Valgrind-3.16.1 and LibVEX; rerun with -h for copyright info
==7397== Command: ./am
==7397== 
==7397== Warning: client switching stacks?  SP change: 0x1ffefffe30 --> 0x1ffa3b4a30
==7397==          to suppress, use: --max-stackframe=80000000 or greater
==7397== Invalid write of size 8
==7397==    at 0x1092E1: arrayManipulation(int, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >) (in /home/krillxox/Documents/CPP/am)==7397==  Address 0x1ffa3b4a30 is on thread 1's stack
==7397== 
==7397== 
==7397== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==7397==  Access not within mapped region at address 0x1FFA3B4A30
==7397==    at 0x1092E1: arrayManipulation(int, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >) (in /home/krillxox/Documents/CPP/am)
==7397==  If you believe this happened as a result of a stack
==7397==  overflow in your program's main thread (unlikely but
==7397==  possible), you can try to increase the size of the
==7397==  main thread stack using the --main-stacksize= flag.
==7397==  The main thread stack size used in this run was 8388608.
==7397== Invalid write of size 8
==7397==    at 0x482F120: _vgnU_freeres (vg_preloaded.c:57)
==7397==  Address 0x1ffa3b4a28 is on thread 1's stack
==7397== 
==7397== 
==7397== Process terminating with default action of signal 11 (SIGSEGV)
==7397==  Access not within mapped region at address 0x1FFA3B4A28
==7397==    at 0x482F120: _vgnU_freeres (vg_preloaded.c:57)
==7397==  If you believe this happened as a result of a stack
==7397==  overflow in your program's main thread (unlikely but
==7397==  possible), you can try to increase the size of the
==7397==  main thread stack using the --main-stacksize= flag.
==7397==  The main thread stack size used in this run was 8388608.
==7397== 
    ==7397== HEAP SUMMARY:
    ==7397==     in use at exit: 7,276,800 bytes in 200,004 blocks
    ==7397==   total heap usage: 200,004 allocs, 0 frees, 7,276,800 bytes allocated
    ==7397== 
    ==7397== LEAK SUMMARY:
    ==7397==    definitely lost: 0 bytes in 0 blocks
    ==7397==    indirectly lost: 0 bytes in 0 blocks
    ==7397==      possibly lost: 0 bytes in 0 blocks
    ==7397==    still reachable: 7,276,800 bytes in 200,004 blocks
    ==7397==         suppressed: 0 bytes in 0 blocks
    ==7397== Rerun with --leak-check=full to see details of leaked memory
    ==7397== 
    ==7397== For lists of detected and suppressed errors, rerun with: -s
    ==7397== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
    [1]    7396 done                              less ip.txt | 
           7397 segmentation fault (core dumped)  valgrind ./am

我在函数中声明ans数组时出现段错误的原因是什么?

您在堆栈上放置了一个巨大的数组。这是一个有限的资源。你最好使用 std::vector