Mam problem z moim plikiem hash.c w ktorym mam implementacje hashlisty i plik main.c ktory wykorzystuje funkcje z hash.c. Main.c dostarcza dla hash.c dwie funkcje tj. funkcje hashującą i wypisującą elementy aby kod w hash.c byl nieco uniwersalny i mogl przyjmowac rozne typy.
proboje stworzyc biblioteke i odpalic w taki sposob:
#!/bin/bash
gcc -c hash.c -o hash.o
ar r lib_hash.a hash.o
ar tv lib_hash.a
gcc -c main.c -o main.o
gcc main.o lib_hash.a -o result.out
./result.out // <- dostaje Segmentation fault(core dump)
main:
#include <stdio.h>
typedef int my_int;
void printInt(void *e) {
my_int *tmp;
tmp = (my_int *) e;
printf("%d ", *tmp);
}
int generateHashInt(void *value) {
my_int *tmp;
tmp = (my_int *) value;
return (*tmp) % 2;
}
int main() {
struct hash_table *table = prepareHashTable(generateHashInt, printInt);
int a = 10;
int b = 11;
int c = 12;
add(table, (void *) &a); // <------------------- w tym miejscu zaczyna wywalac segmentation fault
add(table, (void *) &b);
add(table, (void *) &c);
printValue(table, (void *) &a);
deleteAll(table);
return 0;
}
kawalek kodu z funkcją 'add':
void add(struct hash_table *hashTable, void *value) {
int hashValue = hashTable->hashFun(value);
if (hashTable->first == NULL) {
hashTable->first = (struct hash_elem *) malloc(sizeof(struct hash_elem));
hashTable->first->key = hashValue;
hashTable->first->values = (struct list_struct *) malloc(sizeof(struct list_struct));
hashTable->first->values->value = value;
return;
}
...reszta kodu do ktorego nie wchodzi
tutaj caly kod, wraz z mainem w jednym pliku i o dziwo w takim rozwiazaniu kod działa
https://ideone.com/bYVgaA
nie moge tego rozgryzc dlaczego to nie dziala gdy chce wykorzystac hash.c w innym pliku :(
debuggowalem funkcje add i z tego co widze, to wszystko sie dobrze alokuje
pomocy!