Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
***“./threads';中出现错误:损坏的双链接列表:fclose中的0x00000000009bb240***_C_Fclose - Fatal编程技术网

***“./threads';中出现错误:损坏的双链接列表:fclose中的0x00000000009bb240***

***“./threads';中出现错误:损坏的双链接列表:fclose中的0x00000000009bb240***,c,fclose,C,Fclose,我需要做一个程序,首先,从一个文本文件中读取一个矩阵,然后把它放到内存中。我可以这样做,但当我尝试关闭包含4行或更多行的矩阵文件时,会出现错误: “./threads”中出现错误:已损坏的双链接列表:0x0000000001b4e240* 读取该文件的代码为: void leitura_matriz1 () { char linha_s[MAX_LINHA]; char *buffer; // leitura da primeira matriz FIL

我需要做一个程序,首先,从一个文本文件中读取一个矩阵,然后把它放到内存中。我可以这样做,但当我尝试关闭包含4行或更多行的矩阵文件时,会出现错误: “./threads”中出现错误:已损坏的双链接列表:0x0000000001b4e240* 读取该文件的代码为:

    void leitura_matriz1 ()
{
    char linha_s[MAX_LINHA];
    char *buffer;

    // leitura da primeira matriz
    FILE * matriz1_file;
    matriz1_file = fopen ("in1.txt","r");
    if (matriz1_file == NULL)
    {
        printf ("Erro na abertura do arquivo\n");
        exit(1);
    }

    // número de linhas
    fgets(linha_s, MAX_LINHA, matriz1_file);
    buffer = strtok(linha_s, " =");
    buffer = strtok(NULL, " =");
    linhas_mat1 = atoi(buffer);

    // número de colunas
    fgets(linha_s, MAX_LINHA, matriz1_file);
    buffer = strtok(linha_s, " =");
    buffer = strtok(NULL, " =");
    colunas_mat1 = atoi(buffer);

    // aloca espaço para a matriz
    matriz1 = (int**) malloc(linhas_mat1 * sizeof(int));
    if (matriz1 == NULL)
    {
        printf("erro memória");
        exit(1);
    }
    int lin, col;
    for (lin = 0; lin < linhas_mat1; lin++)
    {
        matriz1[lin] = (int*) malloc(colunas_mat1 * sizeof(int));
        if (matriz1[lin] == NULL)
        {
            printf ("erro memória 2");
            exit(1);
        }
    }

    // lê os valores do arquivo e coloca na matriz em memória
    for (lin = 0; lin < linhas_mat1; lin++)
    {
        fgets(linha_s, MAX_LINHA, matriz1_file);
        buffer = strtok(linha_s, " ");
        for (col = 0; col < colunas_mat1; col++)
        {
            matriz1[lin][col] = atoi(buffer);
            buffer = strtok(NULL, " ");
        }
    }

    fclose (matriz1_file);  
}
LINHAS是直线和COLUNAS列
我从未在文件仍然打开时关闭文件时出错。只有当文件有3行(4行或更多行)以上时才会发生这种情况。有人知道它可能是什么?

第一次分配不正确。而不是:

matriz1 = (int**) malloc(linhas_mat1 * sizeof(int));
应该是:

matriz1 = (int**) malloc(linhas_mat1 * sizeof(int*));

异常消息(8个字节)似乎表示64位应用程序,因此指针将是8个字节,而
sizeof(int)
可能只有4个字节。这将导致在填充阵列时发生内存覆盖。

非常感谢。这似乎就是问题所在。这是我第一次在64位操作系统中编写C程序,所以这种方法以前一直有效。
matriz1 = (int**) malloc(linhas_mat1 * sizeof(int*));