valgrind堆栈信息分析


今天使用valgrind工具进行内存检查,发现如下错误日志

只看最终结果,实际上没啥内存泄露,但是还是有一些报错,于是仔细分析了一下

==3495== Invalid read of size 4                                                             
==3495==    at 0x400F03: _find_ilde_node (gtc_mempool.c:435)
==3495==    by 0x4011D7: _palloc_block (gtc_mempool.c:571)
==3495==    by 0x400FDB: _palloc_small (gtc_mempool.c:481)
==3495==    by 0x400B57: gtc_palloc (gtc_mempool.c:255)
==3495==    by 0x400BA8: gtc_pcalloc (gtc_mempool.c:281)
==3495==    by 0x401996: test3 (test.c:125)
==3495==    by 0x401B44: main (test.c:199)
==3495==  Address 0x5c41080 is 0 bytes after a block of size 2,048 alloc'd
==3495==    at 0x4C2C2A5: memalign (vg_replace_malloc.c:908)
==3495==    by 0x4C2C36F: posix_memalign (vg_replace_malloc.c:1072)
==3495==    by 0x4008B2: gtc_memalign (gtc_alloc.h:48)
==3495==    by 0x40109D: _palloc_block (gtc_mempool.c:508)
==3495==    by 0x400FDB: _palloc_small (gtc_mempool.c:481)
==3495==    by 0x400B57: gtc_palloc (gtc_mempool.c:255)
==3495==    by 0x400BA8: gtc_pcalloc (gtc_mempool.c:281)
==3495==    by 0x401996: test3 (test.c:125)
==3495==    by 0x401B44: main (test.c:199)

该错误表示内存读写越界,"Address 0x5c41080 is 0 bytes after a block of size 2,048 alloc'd"表示内存的分配,"_find_ilde_node (gtc_mempool.c:435)"表示内存越界的地方。

// 使用未初始化的内存
#include                                                               
int main()
{
    int x;
    if(x == 0)
    {
        printf("X is zero");
    }
    return 0;
}

Valgrind提示如下
==14222== Conditional jump or move depends on uninitialised value(s)
==14222== at 0x400484: main (sample2.c:6)
X is zero==14222==
==14222== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 5 from 1)
==14222== malloc/free: in use at exit: 0 bytes in 0 blocks.
==14222== malloc/free: 0 allocs, 0 frees, 0 bytes allocated.
==14222== For counts of detected errors, rerun with: -v
==14222== All heap blocks were freed -- no leaks are possible.

相关