我收到错误消息:

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_sequence\n");
        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};


我的main():

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) = %.7f\n", x, hermite_result);
    printf("Relative error: %.7f\n\n", 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

关于c - 释放的对象的校验和不正确-C中的二维 double 数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48830742/

10-13 07:10