Hello i am trying for two days now to find my mistake and i just can't find it i would be happy if you help me out with it Here is my code: https://pastebin.com/7EGHUsET
I think i have made mistake with the unload function, but i don't understand where can it be? I use a temporary variable just to get the pointer to the next node and the free the current node- am i doing it correct? code for unload
bool unload(void)
{
for (int i = 0; i < N; i++)
{
node *tmp = malloc(sizeof(node));
node *cursor = table[i];
while(cursor -> next != NULL)
{
tmp = cursor;
cursor = cursor -> next;
free(tmp);
}
return false;
valgrind results:
==2921== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==2921== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==2921== Command: ./speller texts/cat.txt
==2921==
==2921== Invalid write of size 8
==2921== at 0x4011E0: load (dictionary.c:72)
==2921== by 0x400964: main (speller.c:40)
==2921== Address 0x30 is not stack'd, malloc'd or (recently) free'd
==2921==
==2921==
==2921== Process terminating with default action of signal 11 (SIGSEGV)
==2921== Access not within mapped region at address 0x30
==2921== at 0x4011E0: load (dictionary.c:72)
==2921== by 0x400964: main (speller.c:40)
==2921== If you believe this happened as a result of a stack
==2921== overflow in your program's main thread (unlikely but
==2921== possible), you can try to increase the size of the
==2921== main thread stack using the --main-stacksize= flag.
==2921== The main thread stack size used in this run was 8388608.
==2921==
==2921== HEAP SUMMARY:
==2921== in use at exit: 552 bytes in 1 blocks
==2921== total heap usage: 1 allocs, 0 frees, 552 bytes allocated
==2921==
==2921== 552 bytes in 1 blocks are still reachable in loss record 1 of 1
==2921== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2921== by 0x5258E49: __fopen_internal (iofopen.c:65)
==2921== by 0x5258E49: fopen@@GLIBC_2.2.5 (iofopen.c:89)
==2921== by 0x40118E: load (dictionary.c:64)
==2921== by 0x400964: main (speller.c:40)
==2921==
==2921== LEAK SUMMARY:
==2921== definitely lost: 0 bytes in 0 blocks
==2921== indirectly lost: 0 bytes in 0 blocks
==2921== possibly lost: 0 bytes in 0 blocks
==2921== still reachable: 552 bytes in 1 blocks
==2921== suppressed: 0 bytes in 0 blocks
==2921==
==2921== For counts of detected and suppressed errors, rerun with: -v
==2921== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
/etc/profile.d/cli.sh: line 94: 2921 Segmentation fault valgrind ./speller texts/cat.txt