valgrind - 地址是分配大小为 16 的块之前的 8 个字节

valgrind - address is 8 bytes before a block of size 16 alloc'd

我在使用 "digesting" valgrind 输出时遇到问题。这是一个片段:

==15145== Invalid write of size 8
==15145==    at 0x40168E: split_node_at_letter (in /home/pgolinski/Dokumenty/Programowanie/git/dictionary/trii)
==15145==    by 0x4018E7: pass_word_further (in /home/pgolinski/Dokumenty/Programowanie/git/dictionary/trii)
==15145==    by 0x401A35: insert_word (in /home/pgolinski/Dokumenty/Programowanie/git/dictionary/trii)
==15145==    by 0x401BD5: main (in /home/pgolinski/Dokumenty/Programowanie/git/dictionary/trii)
==15145==  Address 0x52237d8 is 8 bytes before a block of size 16 alloc'd
==15145==    at 0x4C29BCF: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==15145==    by 0x401063: add_to_trie_word_list (in /home/pgolinski/Dokumenty/Programowanie/git/dictionary/trii)
==15145==    by 0x40173B: pass_word_further (in /home/pgolinski/Dokumenty/Programowanie/git/dictionary/trii)
==15145==    by 0x40183D: pass_word_further (in /home/pgolinski/Dokumenty/Programowanie/git/dictionary/trii)
==15145==    by 0x401906: pass_word_further (in /home/pgolinski/Dokumenty/Programowanie/git/dictionary/trii)
==15145==    by 0x401A35: insert_word (in /home/pgolinski/Dokumenty/Programowanie/git/dictionary/trii)
==15145==    by 0x401BD5: main (in /home/pgolinski/Dokumenty/Programowanie/git/dictionary/trii)

地址是“分配大小为 16 的块之前的 8 个字节”是什么意思?

这意味着 Valgrind 检测到您为您的程序分配(通过 malloc() 或类似方式)的一块内存,并且您尝试访问它之前的 8 个字节的地址。

简而言之,这是一个数组越界错误,您尝试在之前访问数据实际数组数据

incorrect write 表示您为该数据段中的某些内容赋值。例如做 word.length = 4

这是 valgrind 输出的细分:

==15145== Invalid write of size 8
             //This is the function doing the incorrect accessing
==15145==    at 0x40168E: split_node_at_letter (in /home/pgolinski/Dokumenty/Programowanie/git/dictionary/trii)
           [... stack trace ...]

                //This is the address you are trying to access
==15145==  Address 0x52237d8 is 8 bytes before a block of size 16 alloc'd
==15145==    at 0x4C29BCF: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)

             //This is the function that alloc'd the 'block of size 16' (calling malloc as shown in the above line)
==15145==    by 0x401063: add_to_trie_word_list (in /home/pgolinski/Dokumenty/Programowanie/git/dictionary/trii)

           [... stack trace ...]