0

I was solving hackerrank problem https://www.hackerrank.com/challenges/crush/problem. Here's the code:

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];
}

The problem is that it works well on small number of input but when input is ~1000000 it gives segmentation fault and when I declare ans array globally w/ fixed size 10000000 it works with large number of inputs. Here is output of valgrind when I run program with above code:

    ❯ 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

What's the reason of segmentation fault when I declare ans array in my function?

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234

1 Answers1

1

You ar placing a huge array on the stack. This is a limited resource. You are better off using std::vector

pm100
  • 48,078
  • 23
  • 82
  • 145