C 带valgrind误差的矩阵操作

C 带valgrind误差的矩阵操作,c,matrix,valgrind,matrix-multiplication,C,Matrix,Valgrind,Matrix Multiplication,所以我正在制作一个程序,可以从一个二进制输入文件中读取多个矩阵,并将二进制结果写入一个输出文件。我还需要做的是在两个当前矩阵之间进行矩阵乘法,并在乘法后使用排序算法。这是我正在使用的,我提出并承认它不完整: uint64_t diff,writ,comp,rd; struct timespec start, end; struct matrix { int row; int column; }; int compare (const void *a, const void

所以我正在制作一个程序,可以从一个二进制输入文件中读取多个矩阵,并将二进制结果写入一个输出文件。我还需要做的是在两个当前矩阵之间进行矩阵乘法,并在乘法后使用排序算法。这是我正在使用的,我提出并承认它不完整:

uint64_t diff,writ,comp,rd;
struct timespec start, end;


struct matrix {
    int row;
    int column;
};

int compare (const void *a, const void *b)
{
  int x = *(int *)a;
  int y = *(int *)b;

  if (x<y) return -1; 
  if (x>y) return 1; 
  return 0;
}

struct matrix * matrices;

int current[32][32];
int num_argc;
int fd_read;
int fd_write;
int holder = 0;
int i, j, k;
int * fd;
char * input_file;
char * output_file;

void read_matrices() {
    if(num_argc != 3)
    {
        printf("you are missing an input or output file name\n");
        exit(1);
    }
    //this will read in what is inside the binary file and create the desired matrices
    //this will make the size of the matrix
    fd_read = open(input_file, O_RDONLY);
    if(fd_read < 0)
    {
        printf("Failed to read and open the file\n");
        exit(1);
    }
    for ( i = 0; i < 100; i++)
    {
        int rows,column;
        if (!read(fd_read, &rows, sizeof(rows)))
        {
            printf("Can not read in the rows");
            exit(1);
        }
        if (!read(fd_read, &column, sizeof(column)))
        {
            printf("Can not read in the columns");
            exit(1);
        }
        fd = malloc((matrices[i].row * matrices[i].column) * sizeof(* fd));
        matrices[i].row = rows;
        matrices[i].column = column;

        holder = read(fd_read, fd, (matrices[i].row * matrices[i].column) * sizeof(matrices[i]));
    }
    

}

void mult_matrices() {

   // Multiplying first and second matrices and storing it in result
   //then we will also sort each row in ascending order
   for (i = 0; i < matrices[i].column; i++)
   {
       for (j = 0; j < matrices[i].column; j++)
       {

           for (k = 0; k < matrices[i].column; k++)
           {
               current[i][j] = current[i][k] * holder;
           }
           qsort(current[j],matrices[i].column, sizeof(int),compare);
       }
       
   }
   


    //placing the results in the global variable so it can be used in the show function.


}

void show_matrices() {
    // this will show the new matrices after multiplying 
    // this is where we will also sort each row in ascending order
    fd_write = open(output_file, O_CREAT|O_WRONLY,0666);
    if (fd_write < 0)
    {
        printf("Failed to create and open the file\n");
        exit(1);
    }
    if(write(fd_write, *current,(matrices[i].row * matrices[i].column) * sizeof(int)));

    free(fd);
    close(fd_read);
    close(fd_write);
}

int main(int argc, char* argv[]) 
{
    input_file = argv[1];
    output_file = argv[2];
    num_argc = argc;
    //start the elapsed clock
    clock_gettime(CLOCK_REALTIME, &start);

    // initialize any global variables
    //start reading clock
    clock_gettime(CLOCK_REALTIME, &start);
    read_matrices();
    clock_gettime(CLOCK_REALTIME, &end);
    rd = 1000000000 * (end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec;
    //end reading clock


    //start computing clock
    clock_gettime(CLOCK_REALTIME, &start);
    mult_matrices();
    clock_gettime(CLOCK_REALTIME, &end);
    comp = 1000000000 * (end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec;
    //end computing clock

    //start writing clock
    clock_gettime(CLOCK_REALTIME, &start);
    show_matrices();
    clock_gettime(CLOCK_REALTIME, &end);
    writ = 1000000000 * (end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec;
    //end writing clock

    clock_gettime(CLOCK_REALTIME, &end);
    diff = 1000000000 * (end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec;

    // report time
    printf("Reading:       %luns\n", rd);
    printf("Compute:       %luns\n", comp);
    printf("Writing:       %luns\n", writ);
    printf("Elapsed:       %luns\n", diff);

    return 0;
}

矩阵
为空。好吧,我想我解决了空问题。我去掉了所有矩阵的[I],但我收到了一个新的错误
==676== Memcheck, a memory error detector
==676== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.   
==676== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info==676== Command: ./matrix input output
==676==
==676== error calling PR_SET_PTRACER, vgdb might block
==676== Syscall param read(buf) points to unaddressable byte(s)
==676==    at 0x4967142: read (read.c:26)
==676==    by 0x109572: read (unistd.h:44)
==676==    by 0x109572: read_matrices (matrix.c:75)
==676==    by 0x109572: read_matrices (matrix.c:42)
==676==    by 0x109217: main (matrix.c:135)
==676==  Address 0x4a4b040 is 0 bytes after a block of size 0 alloc'd     
==676==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==676==    by 0x109540: read_matrices (matrix.c:71)
==676==    by 0x109540: read_matrices (matrix.c:42)
==676==    by 0x109217: main (matrix.c:135)
==676==
==676== Warning: set address range perms: large range [0x59c9d040, 0x173b11c60) (undefined)
==676== Warning: set address range perms: large range [0x59c9d028, 0x173b11c78) (noaccess)
Can not read in the rows==676== 
==676== HEAP SUMMARY:
==676==     in use at exit: 0 bytes in 0 blocks
==676==   total heap usage: 4 allocs, 4 frees, 4,729,556,524 bytes allocated
==676==
==676== All heap blocks were freed -- no leaks are possible
==676==
==676== For lists of detected and suppressed errors, rerun with: -s       
==676== ERROR SUMMARY: 2 errors from 1 contexts (suppressed: 0 from 0)