0

I am fairly new to c++. For my project I tried to write a class for fast fourier transform based on the FFTW3 library in Ubuntu 17.04. my code is :

my *.hpp file

#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 file:

#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;
}

When I tried to compile the code I faced the issue of "segfault(core dumped)". This problem completely confused me and I can't understand which part of program produced the error .Can anybody tell me how I can fix it? Any help would be highly appreciated.

D.M
  • 510
  • 6
  • 14
Fayyaz
  • 33
  • 4
  • You might start by running your program under a debugger such as gdb. If it's built with debug symbols enabled, you can also use that debugger to load the core file that was dumped, and see a stack trace, variable values, etc. from the crash. Without those details, this is an unreasonably vague question (as opposed to one with a [mcve], which would be stripped down to the shortest possible code that produces a given failure). – Charles Duffy Aug 19 '17 at 05:19
  • Generally speaking, a MCVE won't bother prompting the user for input -- it'll have specific values that are known to reproduce the bug hardcoded. It won't have any logic that isn't part of the shortest possible path to reproduce the bug; it may strip out things that were essential in the context of the original program but are unimportant to demonstrating the issue; etc. – Charles Duffy Aug 19 '17 at 05:23
  • See [How to analyze a program's core dump file with gdb?](https://stackoverflow.com/questions/8305866/how-to-analyze-a-programs-core-dump-file-with-gdb) to collect the information you need to figure out exactly where it failed, and what all your active variables' values were at the time. – Charles Duffy Aug 19 '17 at 05:24

0 Answers0