title: weird malloc : memory corruption
body: <p>I am writing a "simple" code to do FFT. the main issue occurs in the DLpart section:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;math.h&gt;
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;vector&gt;
  #include &lt;string&gt;
  #include &lt;complex&gt;
  #include &lt;algorithm&gt;
  #define SWAP(a,b) tempr=(a);(a) = (b); (b) = tempr
  //although maybe i should make my own swap function rather than a define swap
  
  using namespace std;
  vector&lt;double&gt; bitReversal(vector&lt;double&gt; data, int nn,int* j);
  vector&lt;double&gt; Xcreator(double xSteps);
  vector&lt; double &gt; DLpart(vector&lt;double&gt; data,int nn,int j);
  void arrayGuarder (vector&lt;double&gt;totals, string fileName,double xSteps);
  vector&lt;double&gt; cosineCrafter(double xSteps,double numWaves);
  
  
   main(int argc, char **argv){
  vector&lt;double&gt; input;
  int j = 1;
  double nValues = 8;
  double numWaves = 1; //should not have more than 1 wave per 8 nValues
  vector&lt;double&gt; cosine = cosineCrafter(nValues,numWaves);
  //input = cosine;
  
  vector&lt; double &gt; fouriered;
  input = Xcreator(nValues);
  
  for(int i = 0; i &lt; nValues*2+1; i++)
  {//cout&lt;&lt;input[i]&lt;&lt;endl;
  }
  
  input = bitReversal(input,nValues,&amp;j);
  
  //cout&lt;&lt;input.size()&lt;&lt;endl;
  for(int i = 0; i &lt; nValues*2+1; i++)
  {//cout&lt;&lt;input[i]&lt;&lt;endl;
  }
  fouriered = DLpart(input,nValues,j);
  
  cout&lt;&lt;"gets to here"&lt;&lt;endl;
  
  arrayGuarder(fouriered,"fouriered.dat",nValues);
  }
  
  
  vector&lt;double&gt; Xcreator(double xSteps)
  {
  vector&lt;double&gt; xVector;
  xVector.push_back(0);
  for (double x = 1;x&lt;xSteps+1;x++)
  {
            xVector.push_back(x);
  xVector.push_back(x);
  //cout&lt;&lt;value&lt;&lt;endl;
  }
  return(xVector);
  }
  
  vector&lt;double&gt; bitReversal(vector&lt;double&gt; data, int nn,int* j)
  {
  double tempr;
  double n = nn*2;
  
  double m;
  double counter = 0;
  for(int i = 1; i&lt;n+1 ; i+=2)
  {
  if (*j&gt;i)    
  {SWAP(data[*j],data[i]);    
  SWAP(data[*j+1],data[i+1]);
  counter++;
  }
  m = nn;
  while (m&gt;=2 &amp;&amp; *j&gt;m)
  { *j -= m;    
  m = m/2;
  }
  *j += m;
  }
  //cout&lt;&lt;counter&lt;&lt;endl;
  return(data);
  }
  
  vector&lt;double&gt; DLpart(vector&lt;double&gt; data,int nn,int j)
  {
  double mmax = 2;
  double istep;
  double theta;
  double wpr;
  double wpi;    
  double wr;
  double wi;
  double n = nn*2;
  double isign = 1;
  double wtemp;
  double tempr;
  double tempi;
  while (n&gt;mmax)
  {
  istep = mmax *2;
  theta = isign*(3.1415926535*2/mmax);
  wtemp = sin(.5*theta);
  wpr = -2*wtemp*wtemp;
  wpi = sin(theta);
  wr = 1;
  wi = 0;
  for (int m = 1; m &lt;mmax; m+=2){
  {for (int i = m;i&lt;=n;i+=istep)
  {j = 1+mmax;
  //cout&lt;&lt;mmax&lt;&lt;endl;
  
  tempr = wr*data[j] - wi*data[j+1];
  tempi = wr*data[j+1] + wi*data[j];
  data[j] = data[i]-tempr;
  data[j+1] = data[i+1] - tempi;
  data[i] += tempr;
  data[i+1] += tempi;    
  }
  wr = (wtemp=wr)*wpr-wi*wpi +wr;
  wi = wi*wpr + wtemp*wpi + wi;
  }
  mmax=istep;    
  }
  }
  //cout&lt;&lt;data.size()&lt;&lt;endl;
  for(int l = 0;l&lt;21;l++)
  {//cout&lt;&lt;data[l]&lt;&lt;endl;
  }
  cout&lt;&lt;"gets to here"&lt;&lt;endl;
  return(data);
  }
  void arrayGuarder (vector &lt;double&gt; totals, string fileName,double xSteps)
  {
  string tmp =  string(fileName);
  
  ofstream file;
  file.open ( tmp.c_str() );
  file.precision(6);
  file.setf(ios::fixed | ios::scientific);
  cout.precision(6);
  cout.setf(ios::fixed | ios::scientific);
  
  for (int n = 1; n &lt;xSteps*2 ;n += 2)
  {
  file&lt;&lt;sqrt(totals[n]*totals[n]+totals[n+1]*totals[n+1])&lt;&lt;" ";
  }
  
  }
  
  vector&lt;double&gt; cosineCrafter(double xSteps,double numWaves )
  {
  double xVal;
  vector&lt;double&gt; cosine;
  double y;
  double pi = 3.1415926535;
  
  cosine.push_back(0);//so it starts at 1
                    for (double x = 0;x&lt;xSteps;x++)
  {
  
  y = cos((x/xSteps)*2*pi*numWaves);
  cosine.push_back(y);
  //cout&lt;&lt;y&lt;&lt;endl;
  cosine.push_back(0);
  }
  return(cosine);
  }
  </code></pre>
  
  <p>the main issue that it gets to the first "it gets to here" but has a
  <strong>* glibc detected <em></strong> ./fftTranslated2.o: malloc(): memory corruption: 0x09c1e2c0 *</em>*
  before the second one. how could that be? is anything occuring in between those two steps?
  i did "vector.size" before it went in and as soon as it was about to go out, and they are both 17. Am I doing something else wrong with the vector?</p>
  
  <p>so i ran the program using valgrind but i am unsure of what it is trying to tell me.
  first it states this a few times:</p>
  
  <pre><code>==1544== Invalid write of size 8
  ==1544==    at 0x8049587: DLpart(std::vector&lt;double, std::allocator&lt;double&gt; &gt;, int,    int) (fftTranslated2.cpp:121)
  ==1544==    by 0x8048F8F: main (fftTranslated2.cpp:42)
  ==1544==  Address 0x42d4910 is 8 bytes after a block of size 136 alloc'd
  ==1544==    at 0x402569A: operator new(unsigned int) (vg_replace_malloc.c:255)
  ==1544==    by 0x804A907: __gnu_cxx::new_allocator&lt;double&gt;::allocate(unsigned int,    void const*) (new_allocator.h:89)
  ==1544==    by 0x804A5F7: std::_Vector_base&lt;double, std::allocator&lt;double&gt; &gt;::_M_allocate(unsigned int) (stl_vector.h:140)
  ==1544==    by 0x8049FD1: std::_Vector_base&lt;double, std::allocator&lt;double&gt; &gt;::_Vector_base(unsigned int, std::allocator&lt;double&gt; const&amp;) (stl_vector.h:113)
  ==1544==    by 0x8049B7C: std::vector&lt;double, std::allocator&lt;double&gt; &gt;::vector(std::vector&lt;double, std::allocator&lt;double&gt; &gt; const&amp;) (stl_vector.h:242)
  ==1544==    by 0x8048F6F: main (fftTranslated2.cpp:42)
  </code></pre>
  
  <p>and it finishes with</p>
  
  <pre><code>==1544== HEAP SUMMARY:
  ==1544==     in use at exit: 0 bytes in 0 blocks
  ==1544==   total heap usage: 22 allocs, 22 frees, 10,530 bytes allocated
  ==1544== 
  ==1544== All heap blocks were freed -- no leaks are possible
  ==1544== 
  ==1544== For counts of detected and suppressed errors, rerun with: -v
  ==1544== ERROR SUMMARY: 42 errors from 6 contexts (suppressed: 18 from 7)
  </code></pre>