I am having problems with a piece of code. I have this line in a C program:
mel[i] = malloc(sizeof(double)*mellength[i]);
Where:
double *mel[16];
int mellength[16];
And I need to do this in C++ however I am using the “new” operator:
mel[i] = new double(sizeof(double)*mellength[i]);
Where:
int *mellength = new int[16];
double *mel = new double[16];
And I get the following error:
error: cannot convert ‘double*’ to ‘double’ in assignment
The full function:
void setup_Mel(int fft_size, int sample_rate, int *melstart, int *mellength, double *mel)
{
double fmax;
double dphi;
double fsample;
double freq;
double temp[fft_size/2];
fmax=2595*log10(8000.0f/700+1);
dphi = fmax/17;
freq = (double)sample_rate/fft_size;
for(int i=0; (i < 13); i++)
{
melstart[i] = fft_size/2;
mellength[i] = 0;
memset(temp, 0, sizeof(double)*fft_size/2);
for(int j=0; (j < fft_size/2); j++)
{
fsample = 2595*log10(freq*j/700 + 1);
if((dphi * i <= fsample) && (fsample < dphi*(i+1)))
{
temp[j] = (fsample-dphi*i)/(dphi*(i+1)-dphi*i);
}
if ((dphi*(i+1) <= fsample) && (fsample < dphi*(i+2)))
{
temp[j] = (fsample-dphi*(i+2))/(dphi*(i+1)-dphi*(i+2));
}
if ((temp[j] != 0) && (melstart[i] > j))
{
melstart[i] = j;
}
if (temp[j] != 0) mellength[i]++;
}
mel[i] = new double(sizeof(double)*mellength[i]);
}
}
Hope someone can help 🙂
Doesn’t do what you think – it allocates a single double with the value
sizeof(double)*mellength[i].To allocate an array, you’d need
Sample
The two versions are not equivalent:
in C++ declares an array of
double, whereasdeclares an array of pointers to
double. When you attempt to dereference the C++ variable, you get back adouble, so of course you can’t assign it to the return of anew.