странный malloc: повреждение памяти - PullRequest
0 голосов
/ 14 ноября 2011

Я пишу «простой» код для выполнения 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 Ответ

5 голосов
/ 14 ноября 2011

Вы упомянули glibc, поэтому у вас, вероятно, есть система Linux.

Таким образом, вы могли бы использовать инструмент valgrind для выявления повреждений памяти.

...