C++ malloc(): memory corruption : <p>I am currently going through a fibonacci practice problem on hackerrank and am having a malloc memory corruption error. This is the link to the problem I am doing:</p>

<p><a href="https://www.hackerrank.com/contests/programming-interview-questions/challenges/fibonacci-returns/" rel="nofollow noreferrer">https://www.hackerrank.com/contests/programming-interview-questions/challenges/fibonacci-returns/</a></p>

<p>Input is 0-10, each number separated by a new line.
For each input, the value at that point in the sequence is printed. It works for small inputs, but after 6 it gets the malloc error. It doesn't seem that the size of the sequence is an issue either, just how many are done in succession.</p>

<pre><code>#include &lt;cmath&gt;
#include &lt;cstdio&gt;
#include &lt;vector&gt;
#include &lt;iostream&gt;
#include &lt;algorithm&gt;
using namespace std;

vector&lt;int&gt; bigFib(1);

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int x;
    while(cin &gt;&gt; x){
        if(bigFib.size()-1 &gt;= x){
            cout &lt;&lt; bigFib[x] &lt;&lt; endl;
        }
        else{
            vector&lt;int&gt; fib(x);
            fib[0] = 0;
            fib[1] = 1;
            for(int j = 2; j &lt;= x; j++){
                fib[j] = fib[j-1] + fib[j-2];
            }
            bigFib = fib;
            cout &lt;&lt; fib[x] &lt;&lt; endl;
        }
    }
    return 0;
}
</code></pre>

<p>I am pretty new to C++ and can't find the problem. Thanks for your time.</p>
