为什么会出现 "free(): invalid pointer" 错误?

Why do I get an error of "free(): invalid pointer"?

我是 C 的新手,我正在使用带有 -ansi 的 gcc 编译器。

我正在尝试在 C 中实现用于方阵乘法的 Strassen 算法。作为热身,我只是先实现一个递归算法,它将每个矩阵分解为 4 个子矩阵(我想实现 Strassen 算法之后)。

程序如下:

#include <stdio.h>
#include <stdlib.h>
int** read_square_matrix(int size); /*returns dynamically allocated size x size matrix*/
int** square_matrix_multiply_helper(int** A,int** B, int row1, int col1, int row2, int col2, int size); /*recursively compute product. Assumes size is power of two*/
int** square_matrix_multiply(int** A,int** B, int size);/*expands dimension to a power of two if necessary and computes product*/
int main()
{
    int** A;
    int** B;
    int** C;
    int i,size;

    printf("Enter size: ");
    scanf("%d",&size);

    printf("Enter array A: ");
    A = read_square_matrix(size);

    printf("Enter array B: ");
    B = read_square_matrix(size);

    C = square_matrix_multiply(A,B,size);
    printf("Their product is:\n");
    print_square_matrix(C,size);
    for (i=0;i<size;++i)
    {
        free(A[i]);
        free(B[i]);
        free(C[i]);
    }
    free(A);
    free(B);
    free(C);

    return 0;
}

该代码适用于 size=1,2,4。当我尝试两个 3x3 矩阵时,假设所有 1 都是失败的地方。特别是 free(A[i]) 在第一次迭代时失败,错误为

free(): invalid pointer

现在环顾四周,似乎这意味着 A[i] 不是由 malloc 分配的。我不明白这怎么可能。这是矩阵乘法的代码:

int** square_matrix_multiply(int** A, int** B, int size)
{
    int** C;
    int power,i,j;
    power = 1;
    while (power<size)
        power*=2;
    if (size<power)
    {
        A = realloc(A,power*sizeof(int*));
        B = realloc(B,power*sizeof(int*));
        for (i=0;i<size;++i)
        {
            A[i] = realloc(A[i],power*sizeof(int));
            B[i] = realloc(B[i],power*sizeof(int));
            for (j=size;j<power;++j)
            {
                A[i][j] = 0;
                B[i][j] = 0;
            }
        }
        for (i=size;i<power;++i)
        {
            A[i] = malloc(power*sizeof(int));
            B[i] = malloc(power*sizeof(int));
            for (j=0;j<power;++j)
            {
                A[i][j] = 0;
                B[i][j] = 0;
            }
        }
    }
    C = square_matrix_multiply_helper(A,B,0,0,0,0,power);
    for (i=size;i<power;++i)
    {
        free(A[i]);
        free(B[i]);
        free(C[i]);
    }
    A = realloc(A,size*sizeof(int*));
    B = realloc(B,size*sizeof(int*));
    C = realloc(C,size*sizeof(int*));
    for (i=0;i<size;++i)
    {
        A[i] = realloc(A[i],size*sizeof(int));
        B[i] = realloc(B[i],size*sizeof(int));
        C[i] = realloc(C[i],size*sizeof(int));
    }
    return C;
}

我已经看了好几个小时了,但我不知道哪里出了问题。有谁可以帮助我吗?谢谢! 编辑:实施 read_square_matrix:

int** read_square_matrix(int size)
{
    int** C;
    int i,j;
    C = malloc(size*sizeof(int*));
    for (i=0;i<size;++i)
    {
        C[i] = malloc(size*sizeof(int));
        for (j=0;j<size;++j)
            scanf("%d",&C[i][j]);
    }
    return C;
}

square_matrix_multiply函数中,你应该传递int** Aint** B的地址。将其更改为int** square_matrix_multiply(int*** A, int*** B, int size)C = square_matrix_multiply(A,B,size)应该是C = square_matrix_multiply(&A,&B,size),改变square_matrix_multiply的工具。