奇怪的malloc:内存损坏

时间:2011-11-13 20:59:35

标签: c++ malloc fft corruption

我正在编写一个“简单”代码来进行FFT。主要问题发生在DLpart部分:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <complex>
#include <algorithm>
#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<double> bitReversal(vector<double> data, int nn,int* j);
vector<double> Xcreator(double xSteps);
vector< double > DLpart(vector<double> data,int nn,int j);
void arrayGuarder (vector<double>totals, string fileName,double xSteps);
vector<double> cosineCrafter(double xSteps,double numWaves);


 main(int argc, char **argv){
vector<double> input;
int j = 1;
double nValues = 8;
double numWaves = 1; //should not have more than 1 wave per 8 nValues
vector<double> cosine = cosineCrafter(nValues,numWaves);
//input = cosine;

vector< double > fouriered;
input = Xcreator(nValues);

for(int i = 0; i < nValues*2+1; i++)
{//cout<<input[i]<<endl;
}

input = bitReversal(input,nValues,&j);

//cout<<input.size()<<endl;
for(int i = 0; i < nValues*2+1; i++)
{//cout<<input[i]<<endl;
}
fouriered = DLpart(input,nValues,j);

cout<<"gets to here"<<endl;

arrayGuarder(fouriered,"fouriered.dat",nValues);
}


vector<double> Xcreator(double xSteps)
{
vector<double> xVector;
xVector.push_back(0);
for (double x = 1;x<xSteps+1;x++)
{
          xVector.push_back(x);
xVector.push_back(x);
//cout<<value<<endl;
}
return(xVector);
}

vector<double> bitReversal(vector<double> data, int nn,int* j)
{
double tempr;
double n = nn*2;

double m;
double counter = 0;
for(int i = 1; i<n+1 ; i+=2)
{
if (*j>i)    
{SWAP(data[*j],data[i]);    
SWAP(data[*j+1],data[i+1]);
counter++;
}
m = nn;
while (m>=2 && *j>m)
{ *j -= m;    
m = m/2;
}
*j += m;
}
//cout<<counter<<endl;
return(data);
}

vector<double> DLpart(vector<double> 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>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 <mmax; m+=2){
{for (int i = m;i<=n;i+=istep)
{j = 1+mmax;
//cout<<mmax<<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<<data.size()<<endl;
for(int l = 0;l<21;l++)
{//cout<<data[l]<<endl;
}
cout<<"gets to here"<<endl;
return(data);
}
void arrayGuarder (vector <double> 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 <xSteps*2 ;n += 2)
{
file<<sqrt(totals[n]*totals[n]+totals[n+1]*totals[n+1])<<" ";
}

}

vector<double> cosineCrafter(double xSteps,double numWaves )
{
double xVal;
vector<double> cosine;
double y;
double pi = 3.1415926535;

cosine.push_back(0);//so it starts at 1
                  for (double x = 0;x<xSteps;x++)
{

y = cos((x/xSteps)*2*pi*numWaves);
cosine.push_back(y);
//cout<<y<<endl;
cosine.push_back(0);
}
return(cosine);
}

它到达第一个“它到达这里”的主要问题,但有一个 * glibc检测到 ./fftTranslated2.o:malloc():内存损坏:0x09c1e2c0 * * 在第二个之前。怎么会这样?这两个步骤之间发生了什么? 我在进入之前做了“vector.size”,一旦它即将退出,它们都是17.我对载体做了其他错误吗?

所以我使用valgrind运行程序,但我不确定它试图告诉我什么。 首先它说了几次:

==1544== Invalid write of size 8
==1544==    at 0x8049587: DLpart(std::vector<double, std::allocator<double> >, 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<double>::allocate(unsigned int,    void const*) (new_allocator.h:89)
==1544==    by 0x804A5F7: std::_Vector_base<double, std::allocator<double> >::_M_allocate(unsigned int) (stl_vector.h:140)
==1544==    by 0x8049FD1: std::_Vector_base<double, std::allocator<double> >::_Vector_base(unsigned int, std::allocator<double> const&) (stl_vector.h:113)
==1544==    by 0x8049B7C: std::vector<double, std::allocator<double> >::vector(std::vector<double, std::allocator<double> > const&) (stl_vector.h:242)
==1544==    by 0x8048F6F: main (fftTranslated2.cpp:42)

并以

结束
==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)

1 个答案:

答案 0 :(得分:5)

你提到了glibc,所以你可能有一个Linux系统。

因此,您可以使用valgrind工具来查找内存损坏。