-1

I've been working on Microsoft Visual C++ 2010(32-bit system)

The compilation phase is alright, but on execution I get an error that says:

"Windows has triggered a breakpoint in Espectrogramafrequencia.exe. This may be due to corruption of the heap, which indicates a bug in Espectrogramafrequencia.exe or any of the DLLs it has loaded-. This may also be due to user pressing F12 while Espectrogramafrequencia.exe has focus. The output window may have more diagnostic information. [BREAK] [CONTINUE] [IGNORE]"

Code:

#include "StdAfx.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <fftw3.h>
#include <iostream>
#include <cmath>
#include <fstream>

using namespace std;

int main()
{
int i;
const int N=550;//Number of points acquired inside the window
double Fs=200;//sampling frequency
double dF=Fs/N;
double  T=1/Fs;//sample time 
double f=50;//frequency
double *in;
fftw_complex *out;
double t[N];//time vector 
double ff[N];
fftw_plan plan_forward;
in = (double*) fftw_malloc(sizeof(double) * N);
out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);
for (int i=0; i<= N;i++) 
{
t[i]=i*T;
double M_PI=3.14159265359;
in[i] =0.7 *sin(2*M_PI*f*t[i]);// generate sine waveform
double multiplier = 0.5 * (1 - cos(2*M_PI*i/(N-1)));//Hanning Window
in[i] = multiplier * in[i];
}
for (int i=0; i<= ((N/2)-1);i++){
ff[i]=Fs*i/N;}
plan_forward = fftw_plan_dft_r2c_1d ( N, in, out, FFTW_ESTIMATE );
fftw_execute ( plan_forward );
double v[N];
for (int i = 0; i<= ((N/2)-1); i++){
v[i]=(20*log(sqrt(out[i][0]*out[i][0]+ out[i][1]*out[i][1])))/N;  //Here   I have calculated the y axis of the spectrum in dB
}
fstream fichero;
fichero.open("example2.txt",fstream::out);//fichero.open("example2.txt");
fichero << "plot '-' using 1:2" << std::endl;//fichero << "plot '-' using 1:2" << endl;
for(i = 0;i< ((N/2)-1); i++){ 
fichero << ff[i]<< " " << v[i]<< std::endl;
}
fichero.close();
fftw_destroy_plan (plan_forward);
fftw_free (in);
fftw_free (out);
return 0;
}

Have you any idea of what this error means? What does "corruption of the heap" mean?

Please help

Thank you so much!

  • 3
    You've modified some memory that wasn't yours, in the vicinity of something that was dynamically allocated. Watch your indexing. – molbdnilo Apr 19 '17 at 19:57
  • 3
    The loop indexing `for (int i=0; i<= N;i++)` is suspect. That means you will iterate `N+1` times, which is bad news if you're dealing with a block of `N` elements. – Phil Brubaker Apr 19 '17 at 19:58

1 Answers1

1

You are trying to assign value outside of t[] memory limit.
C++ arrays are zero based index, so the index range of array t[] is 0..549.
Use a for loop like:

for (int i=0; i< N;i++) 
legoscia
  • 39,593
  • 22
  • 116
  • 167