2011-11-13 206 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,但我不確定它試圖告訴我什麼。我把它的通知添加到我原來的帖子 –