免费释放对象的校验和不正确



我正在收到错误:

malloc: *** error for object 0x100502048: incorrect checksum for freed      
object - object was probably modified after being freed.

问题是,此错误随机发生。有时该程序执行,我得到我正在寻找的答案,有时会弹出此错误。

我正在使用Xcode进行调试,它指向此功能定义:

double **Hermite_coeff(double *z, double *output, double *deriv, int n)
{
    int i, j;
    double **H;
    H = calloc(2*n, sizeof(double*)); // <-----Error points to here
    for (i = 0; i < 2*n; ++i)
        H[i] = calloc((i+1),sizeof(double));
    for (i = 0; i < n; ++i)
    {
        H[2*i][0] = output[i];
        H[2*i+1][0] = output[i];
        H[2*i+1][1] = deriv[i];
        if (i != 0)
        {
            H[2*i][1] = (H[2*i][0] - H[2*i-1][0])/(z[2*i] - z[2*i-1]);
        }
    }
    for (i = 2; i < 2*n; ++i)
    {
        for (j = 2; j <= i; j++)
        {
            H[i][j] = (H[i][j-1] - H[i-1][j-1])/(z[i] - z[i-j]);
        }
    }
    return H;
}

这是生成双 *z。

的函数
double *Hermite_z_sequence(double *input, int n)
{
    int i;
    double *z;
    if ((z = calloc(2*n, sizeof(double))) == NULL)
    {
        printf("Malloc failed in Hermite_z_sequencen");
        return NULL;
    }
    for (i = 0; i < 2*n; ++i)
    {
        z[2*i] = input[i];
        z[2*i+1] = input[i];
    }
    return z;
}

这最终是我要运行的。

double Hermite_interpolation(double *z, double **coeff, int n, double x)
{
    int i, j;
    double result, sum;
    result = coeff[0][0];
    for (i = 1; i < 2*n; i++)
    {
        sum = 1;
        for (j = 1; j <= i; j++)
            sum *= (x - z[j-1]);
        result += (coeff[i][i]*sum);
    }
    return result;
}

这就是我定义输入,输出和派生的方式:

// Input
double input[] = {0.30, 0.32, 0.35};
// Output
double sin_x[] = {0.29552, 0.31457, 0.34290};
// Derivative of sin_x
double cos_x[] = {0.95534, 0.94924, 0.93937};

我的主((:

int main(int argc, char **argv)
{
    // initializing the given parameters for the assignment
    int n;
    double actual_output, x, *z, **h_coeff, hermite_result;
    double input[] = {0.30, 0.32, 0.35};
    double sin_x[] = {0.29552, 0.31457, 0.34290};
    double cos_x[] = {0.95534, 0.94924, 0.93937};
    n = 3;
    x = 0.34;
    z = Hermite_z_sequence(input, n);
    h_coeff = Hermite_coeff(z, sin_x, cos_x, n);
    hermite_result = Hermite_interpolation(z, h_coeff, n, x);
    actual_output = sin(x);
    printf("Hermite H_5(%.2f) = %.7fn", x, hermite_result);
    printf("Relative error: %.7fnn", relative_error(actual_output, hermite_result));
    h_coeff = destroy_diagonal_2D_array(h_coeff, 2*n);
    free(z);
    return 0;
}

有时会显示:

Hermite H_5(0.34) = 0.3334889
Relative error: 0.0000054

在其他时候这显示了:

malloc: *** error for object 0x1004090e8: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug
(lldb) 

请看一下,告诉我它看起来对您是否正确:

for (i = 0; i < 2*n; ++i) 
{
    z[2*i] = input[i]; 
    z[2*i+1] = input[i]; 
} 

假设我们有

z = calloc(2*n, sizeof(double)) 

在该循环中,您超过了2*n。在for的条件下,您可能是要编写i < n而不是i < 2*n

相关内容

  • 没有找到相关文章

最新更新