title: Memory error: free(): invalid next size (fast)
body: <p>I'm trying to implement a <code>radix sort</code>, and this code produce a memory failure: </p>
  
  <pre><code>(free(): invalid next size (fast))
  </code></pre>
  
  <p>The code is the following:</p>
  
  <pre><code>unsigned int * radix(unsigned int *x,int n, int g,bool verbose)
  {
      int elem_size = sizeof(unsigned int)*8;
      int mask = ((1&lt;&lt;g)-1);    
      float num_rounds= ((float)elem_size/(float)g);
      int B = 1 &lt;&lt; g; // 2^g BTW
  
      vector&lt;unsigned int&gt; buckets[B];
  
      // begin radix sort
      for( unsigned int round=0 ; round&lt;num_rounds ; ++round)
        {
  
          // count
          for(int elem=0 ; elem&lt;n ; ++elem)
          {
              // calculate the bucket number:
              unsigned int const bucket_num = ( x[elem] &gt;&gt; g*round) &amp; mask;
      ---&gt;        buckets[bucket_num].push_back(x[elem]);
          }
          // more stuff
      }
      return x;
  }
  </code></pre>
  
  <p><code>GDB</code> states that the error is inside push_back, but <code>elem</code> is always smaller than <code>n</code> (where <code>n</code> is the size of <code>x[]</code>). So, I thought that it could only be on <code>bucket_num</code>. However, just before it crashes <code>GDB</code> gives me their values:</p>
  
  <blockquote>
  <pre><code>Breakpoint 1, radix (x=0x604010, n=50, g=4, verbose=true)
      at radix.mpi.seq.cpp:38
  38                buckets[bucket_num].push_back(x[elem]);
  2: B = 16
  1: bucket_num = 2
  </code></pre>
  </blockquote>
  
  <p>any ideas?</p>