Building problems using Arrays

Hi, I started programming in C++ two weeks ago, I am working now on DSP with C++ (I have Matlab experience), and I'm stuck with something. I use Eclipse and Win7, I am designing a fft-filter from an incoming wav recording + IR, and I get the following lines in the building process:

0 [main] democpp1 4424 exception::handle: Exception: STATUS_ACCESS_VIOLATION
784 [main] democpp1 4424 open_stackdumpfile: Dumping stack trace to democpp1.exe.stackdump

This is the class where the filter is done:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <fftw3.h>
#include "fftfilt.h"
#include <cmath>
#define min(a,b) (((a) < (b)) ? (a):(b))


void fftfilt::compute(double *dataArray, int sizeArray, double *irArray, int sizeIR){

	//Variables
	int nx = sizeArray;
	int nb = sizeIR;
	int nfft = 2048;


	//FFT size to a power of 2 for speed
	int ppw = 0;
	while (nfft<nb){
		nfft = pow(2,ppw);
		ppw++;
	}

    int L = nfft - nb + 1;

    //FFT of IR
    fftw_complex *out_cpx_ir;
    fftw_plan fft_ir;
    out_cpx_ir = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*nfft);
    fft_ir = fftw_plan_dft_r2c_1d(nfft, irArray, out_cpx_ir, FFTW_ESTIMATE);
    fftw_execute(fft_ir);


    //Result allocation
    double *y;
    y = (double *) malloc(sizeArray*sizeof(double));


    //More variables
    int istart = 0;
    int iend;

    fftw_complex *out_cpx_voice;
    fftw_plan fft_voice;
    out_cpx_voice = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*nfft);



    fftw_complex *out_cpx_total;
    out_cpx_total = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*nfft);

    double *out_local;
    out_local = (double *) malloc(nfft*sizeof(double));
    fftw_plan ifft;
    ifft = fftw_plan_dft_c2r_1d(nfft, out_cpx_total, out_local, FFTW_ESTIMATE);

    //Filtering in Freq domain
    while (istart <= nx){

        iend = min(istart+L-1,nx);

        //Local data - save memory
        double *x;
        x = (double *) malloc((iend-istart+1)*sizeof(double));

        int index =0;
        for(int i=istart;i<iend;i++){
        	x [index] = dataArray[i];
        	index++;
        }

        fft_voice = fftw_plan_dft_r2c_1d(nfft, x, out_cpx_voice, FFTW_ESTIMATE);
        fftw_execute(fft_voice);

        for(int j=1; j<nfft;j++){
        	out_cpx_total[j][0] = out_cpx_voice[j][0]*out_cpx_ir[j][0];
        	out_cpx_total[j][1] = out_cpx_voice[j][1]*out_cpx_ir[j][1];
        }


        fftw_execute(ifft);

          for (int i=0;i<nfft;i++){
        	  out_local[i] = out_local[i]/nfft;
         }

         int yend = min(nx,istart+nfft-1);

         int loc_pos = 0;
         int p;
         for (p=istart;p<yend;p++){
        	 y[p] = y[p] + out_local[loc_pos];
        	 loc_pos++;
         }

         istart = istart + L;
         finalsize(p);
    }


    finalarray(y);


}


Am I doing something wrong with the array, memory or something?

Thanks in advance
This is what's usually referred to as a segFault; you're reading/writing memory that isn't yours. Does the error indicate the line where the error occurs? If not, can you build with debug symbols and run again, to get that information?

The alternative is to run it under a debugger, which will catch it at the moment of error.

As an aside, in C++, prefer new/delete to malloc/free, and prefer the C++ math functions in cmath to the C math functions in math.h (I see you've included both).
This is what I get in the debug mode, don't know if it helps:



[New Thread 4888.0x1334]
[New Thread 4888.0x10b4]
warning: (Internal error: pc 0xc7 in read in psymtab, but not in symtab.)

warning: (Internal error: pc 0xc7 in read in psymtab, but not in symtab.)
If you compile with the -g option (for g++ at least) it should tell you the line number where the error occurs ... or at least the line number where it noticed that an error occurs! Can you show us your compile command and a cut-and-paste of a command line execution attempt?
Topic archived. No new replies allowed.