Segfault(核心倾销)

时间:2017-08-19 05:12:33

标签: segmentation-fault

我对c ++很新。对于我的项目,我尝试根据Ubuntu 17.04中的FFTW3库编写一个快速傅里叶变换类。我的代码是:

我的* .hpp文件

#include <iostream>
#include <cmath>
#include "fftw3.h"
#include <complex>
#ifndef FG_HPP
#define FG_HPP

class myfft
  {
    public:
    int n;
    fftw_complex *x, *y;
    fftw_plan p;
    void setvalue() ;
    void fftforward();
    void fftbackward();
    void mfree();
    myfft(int );
    ~myfft();
  };

 #endif

my * .cpp文件:

#include "fg.hpp"
using namespace std;

//Constructure
myfft::myfft(int a){
n = a;
x = new fftw_complex [n]; // <= allocate array
y = new fftw_complex [n];

for (int i = 0; i<n; i++) {
    x[i][0] = i+1;
    x[i][1] = i+i;

    //y[i][0] = i+1;
    //y[i][1] = i+i;
    }
 }

 //---------------------------------------------------   
//setvalue
void myfft::setvalue(){

 for (int i = 0; i<n; i++) {
      cout<<"Please enter real  "<<i+1<<endl;
      cin>> x[i][0];
      cout<<"Please enter image  "<<i+1<<endl;
      cin>> x[i][1];
   }
}

//---------------------------------------------------   
// forward mapping
void myfft::fftforward()
{
    p=fftw_plan_dft_2d(n, n, x, y, FFTW_FORWARD, FFTW_ESTIMATE);
    fftw_execute(p);
}

//-----------------------------------------------------------
// backward mapping
void myfft::fftbackward()
{
    p=fftw_plan_dft_2d(n, n, x, y, FFTW_BACKWARD, FFTW_ESTIMATE);
    fftw_execute(p);
}

//-----------------------------------------------------------
// Destructor and clean up
myfft::~myfft(){
  if (x) {delete[] x;}
  if (y) {delete[] y;}
}

void myfft::mfree(){fftw_destroy_plan(p); fftw_cleanup();}


 //-----------------------------------------------------------
 // Main Program
int main(int argc, char** argv) 
{
myfft* g;
g = new myfft(25);       // enter number of array as argument

if (!g) {
   cout << "Not enough memory available or error in allocation.\n";
   return(-1);
}

g->setvalue();
g->fftforward();
g->mfree();

for(int i=0; i<5; i++){
    cout << g->x[i][0] 
         <<"  "
         << g->x[i][1]
         << endl;
}
for(int i=0; i<5; i++){
    cout << g->y[i][0] 
         << "   " 
         << g->y[i][1]
         << endl;
}

return 0;
}

当我尝试编译代码时,我遇到了“segfault(core dumped)”的问题。这个问题让我很困惑,我无法理解程序的哪个部分产生错误。可以告诉我如何修复它?任何帮助都将受到高度赞赏。

0 个答案:

没有答案