2011-11-13 156 views
-1

我正在写一个“简单”的代码来做FFT。在DLpart段中出现的主要问题:奇怪malloc:内存损坏

#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) 
+5

您需要将其压缩为一小段代码,展现您想要的行为......我怀疑任何人都想为您调试整个应用程序。 – SoapBox

+3

请缩进您的代码。并将其切成最小样本。 –

+0

Re。交换:使用'std :: swap'。 – GManNickG

回答

5

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

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

+0

“valrind”的+1。总是使用'valgrind'。永远不要张贴在SO之前'valground'有点:-) –

+0

谢谢staryn,我会尝试使用valgrind。就像我说的那样,这个问题似乎发生在DLpart函数中,在它退出该函数之前它的权利很好,但一旦退出,它就有内存损坏权利。 –

+0

我试图使用valgrind,但我不确定它试图告诉我什么。我把它的通知添加到我原来的帖子 –