HASH_SIZE = 10
int hash_int(int id){
return id % HASH_SIZE;
}
С идентификаторами, которые у меня есть в файле, значения hash_int go от 0 до 9.
Это моя функция вставки:
void hash_insert_edge( CollList** hash, int id, int target, float peso){
int pos = hash_int(id);
hash[pos] = hash_insert_edge_cl(hash[pos], id, target, peso);
}
CollList* hash_insert_edge_cl(CollList* lst, int id, int target, float peso){
if(lst && lst->id == id){
//node already exists adding new incidence
lst->edges = insert_incidence(lst->edges,target,peso);
}else if (lst && lst->id < id)
{
//ordered insertion
lst->next_coll = hash_insert_edge_cl(lst->next_coll, id, target, peso);
}else
{
//new node insertion
CollList* new = (CollList*) malloc(sizeof(CollList));
new->id = id;
new->cidade_origem = NULL;
new->next_coll = lst;
//new->edges = NULL;
new->edges = insert_incidence(NULL,target,peso);
lst = new;
}
return lst;
}
Это работает с идентификаторами go через функцию hash_int и принадлежит [1,9], но если он равен 0, они не сохранятся. Пытался изменить возвращаемое значение hash_int на + 1 (например, вместо возврата 0 он вернет 1 и будет на следующей позиции), и это сработало, данные теперь хранятся правильно. Может это проблема с памятью? Написание вне пределов? Я уже использовал valgrind, и это результат:
==2934== Use of uninitialised value of size 8
==2934== at 0x109D41: main (in /home/diogo/Desktop/Codigo/test)
==2934==
==2934== Invalid read of size 4
==2934== at 0x109D41: main (in /home/diogo/Desktop/Codigo/test)
==2934== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==2934==
==2934==
==2934== Process terminating with default action of signal 11 (SIGSEGV)
==2934== Access not within mapped region at address 0x0
==2934== at 0x109D41: main (in /home/diogo/Desktop/Codigo/test)
==2934== If you believe this happened as a result of a stack
==2934== overflow in your program's main thread (unlikely but
==2934== possible), you can try to increase the size of the
==2934== main thread stack using the --main-stacksize= flag.
==2934== The main thread stack size used in this run was 8388608.
Contador 1: 10084435 | Contador 2: 15493==2934==
==2934== HEAP SUMMARY:
==2934== in use at exit: 86,470 bytes in 5,357 blocks
==2934== total heap usage: 5,362 allocs, 5 frees, 96,630 bytes allocated
==2934==
==2934== LEAK SUMMARY:
==2934== definitely lost: 32 bytes in 1 blocks
==2934== indirectly lost: 9,487 bytes in 589 blocks
==2934== possibly lost: 0 bytes in 0 blocks
==2934== still reachable: 76,951 bytes in 4,767 blocks
==2934== suppressed: 0 bytes in 0 blocks
==2934== Rerun with --leak-check=full to see details of leaked memory
==2934==
==2934== Use --track-origins=yes to see where uninitialised values come from
==2934== For lists of detected and suppressed errors, rerun with: -s
==2934== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
Segmentation fault (core dumped)