免费释放对象的校验和不正确 - C中的2D双数组

时间:2018-02-16 16:14:18

标签: c arrays memory malloc

我收到了错误:

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;
}

这是生成double * 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};

我的主要():

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) 

1 个答案:

答案 0 :(得分:1)

看看这个,并告诉我它是否适合你:

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