Valgrind错误“无效写入大小4”C

时间:2015-05-31 13:00:13

标签: c valgrind

我正在使用C编程,当我使用Valgrind检查内存错误时,下一个错误显示:

==9756== Invalid write of size 4
==9756==    at 0x40164D: main (flowTracker.c:294)
==9756==  Address 0x24 is not stack'd, malloc'd or (recently) free'd

flowTracker.c的第294行是下一个:

tabla_hash[clave_hash]->contador++;

tabla_hash的声明是:

#define TAMANHO_TABLA 1048576

typedef struct{

    int tiempo_ini;
    int tiempo_ult;
    uint8_t quintupla[13];
    int num_bytes;
    int num_SYN;
    int num_ACK;
    int contador;
    double pack_s;
    double bits_s;
} FlujoIP;

FlujoIP *tabla_hash[TAMANHO_TABLA];

1 个答案:

答案 0 :(得分:3)

正如4566976所指出的,tabla_hash[clave_hash]是(可能是NULL。这只是一个猜测,因为你还没有提供MCVE来重现问题,而我们不必填写空白或修复编译器错误......

在我看来,好像你可能是这样声明tablahashFlujoIP tabla_hash[TAMANHO_TABLA];(虽然,哇!这是一个巨大的数组)......你应该然后可以将->改为.,如下所示:tabla_hash[clave_hash].contador++;

或者,如果您在违规声明之前加上if (tablahash[clave_hash] == NULL) { tablahash[clave_hash] = malloc(sizeof tablahash[clave_hash][0]); }或其他内容,那么也可能是合适的......不要忘记free 中的所有项目数组。