繁体   English   中英

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

[英]Valgrind Error “Invalid write of size 4” C

我正在用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];

正如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]); } if (tablahash[clave_hash] == NULL) { tablahash[clave_hash] = malloc(sizeof tablahash[clave_hash][0]); }东西,也可能合适……不要忘记free 庞大数组中的所有项目。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM