Valgrind错误结构

时间:2015-05-28 23:32:04

标签: c valgrind

我试图在juantest.txt文件中编写和读取一个简单的struct节点。但是当我使用valgrind验证此代码时,我收到了以下错误。你能帮我解决这个问题吗?这里发生了什么?。

 ==25771== 4 errors in context 99 of 100:
 ==25771== Invalid read of size 4
 ==25771==    at 0x4EA5973: fread (libioP.h:905)
 ==25771==    by 0x40080D: validation_read (in /home/grados-sanchez/git/merkle-codigos-C/test)
 ==25771==    by 0x40087E: node_read (in /home/grados-sanchez/git/merkle-codigos-C/test)
 ==25771==    by 0x400B18: main (in /home/grados-sanchez/git/merkle-codigos-C/test)
 ==25771==  Address 0x51fc120 is 224 bytes inside a block of size 568 free'd
 ==25771==    at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
 ==25771==    by 0x4EA4AE4: fclose@@GLIBC_2.2.5 (iofclose.c:85)
 ==25771==    by 0x400ACC: main (in /home/grados-sanchez/git/merkle-codigos-C/test)
 ==25771== 
 ==25771== 
 ==25771== 4 errors in context 100 of 100:
 ==25771== Invalid read of size 4
 ==25771==    at 0x4EA5951: fread (libioP.h:905)
 ==25771==    by 0x40080D: validation_read (in /home/grados-sanchez/git/merkle-codigos-C/test)
 ==25771==    by 0x40087E: node_read (in /home/grados-sanchez/git/merkle-codigos-C/test)
 ==25771==    by 0x400B18: main (in /home/grados-sanchez/git/merkle-codigos-C/test)
 ==25771==  Address 0x51fc124 is 228 bytes inside a block of size 568 free'd
==25771==    at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==25771==    by 0x4EA4AE4: fclose@@GLIBC_2.2.5 (iofclose.c:85)
==25771==    by 0x400ACC: main (in /home/grados-sanchez/git/merkle-codigos-C/test)

这是我的代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define id_lenght 6000
typedef unsigned char * ustring;
typedef struct {
    ustring ustr;
    int height;
    char id[id_lenght];
} node;

int validation_read(void * ptr_var, size_t sizeof_datatype, int num,
    FILE * ptr_file) {
    if (fread(ptr_var, sizeof_datatype, num, ptr_file) <= 0) {
        printf("Error reading file");
       return 1;
    }
return 0;
}

void node_read(FILE * node_ptr, node * n, int r) {
    int i;
    validation_read(n->id, sizeof(unsigned char), id_lenght, node_ptr);
    validation_read(&(n->height), sizeof(int), 1, node_ptr);
    validation_read(n->ustr, sizeof(unsigned char) * (r + 1), 1,node_ptr);
}

void node_init(node * n, int r) {
    memset(n, 0, sizeof(node));
    n->ustr = malloc((r + 1) * sizeof(unsigned char));
    memset(n->ustr, 0, (r + 1));
    n->height = -1;
    memset(n->id,0,id_lenght);
}


void node_write(FILE * node_ptr, node * n, int r) {
    int i;
    char newid[id_lenght];
    memset(newid,0,id_lenght);
    sprintf(newid,"%s",n->id);
    fwrite(newid, sizeof(unsigned char), id_lenght, node_ptr);
    fwrite(&(n->height), sizeof(int), 1, node_ptr);
    fwrite(n->ustr, sizeof(unsigned char) * (r + 1), 1,node_ptr);
}


void node_destroy(node * n) {
    free(n->ustr);
    n->height = -1;
}

int main() {
    FILE * ptr = fopen("juantest", "w+");
    int r = 64 / 8;
    node in;
    node_init(&in, r);
    node_write(ptr, &in, r);
    node_destroy(&in);
    fclose(ptr);
    FILE * ptr1 = fopen("juantest", "r");
    node in1;
    node_init(&in1, r);
    node_read(ptr, &in1, r);
    node_destroy(&in1);
    fclose(ptr1);
}

1 个答案:

答案 0 :(得分:1)

node_read(ptr, &in1, r);

ptr已经关闭。我想你的意思是:

node_read(ptr1, &in1, r);