היי,
valgrind אכן מוצא שגיאות זכרון, כולל קריאה לזכרון לא מאותחל, או גישה לאזור זכרון לא מוקצה וכדומה.
הדוגמא מטה קוראת לערך לא מאותחל של משתנה m:
#include <stdlib.h>
#include <stdio.h>
int main () {
int* m = malloc(sizeof(*m));
if (m == NULL)
return 0;
printf("Should be error: %d\n", *m);
return 0;
}
הפלט בשרת הוא:
אבל כשמריצים valgrind הוא כן מחזיר המון שגיאות (כי הערך לא אותחל לפני הגישה למשתנה) - סימנתי ב-bold את השגיאה הרלוונטית:
==25503== Memcheck, a memory error detector
==25503== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==25503== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==25503== Command: ./main
==25503==
==25503== Conditional jump or move depends on uninitialised value(s)
==25503== at 0x4E80C4E: vfprintf (in /usr/lib64/libc-2.17.so)
==25503== by 0x4E89498: printf (in /usr/lib64/libc-2.17.so)
==25503== by 0x4005B7: main (in /home/ortalcohen/y/main)
==25503==
==25503== Use of uninitialised value of size 8
==25503== at 0x4E7E31B: _itoa_word (in /usr/lib64/libc-2.17.so)
==25503== by 0x4E825A0: vfprintf (in /usr/lib64/libc-2.17.so)
==25503== by 0x4E89498: printf (in /usr/lib64/libc-2.17.so)
==25503== by 0x4005B7: main (in /home/ortalcohen/y/main)
==25503==
==25503== Conditional jump or move depends on uninitialised value(s)
==25503== at 0x4E7E325: _itoa_word (in /usr/lib64/libc-2.17.so)
==25503== by 0x4E825A0: vfprintf (in /usr/lib64/libc-2.17.so)
==25503== by 0x4E89498: printf (in /usr/lib64/libc-2.17.so)
==25503== by 0x4005B7: main (in /home/ortalcohen/y/main)
==25503==
==25503== Conditional jump or move depends on uninitialised value(s)
==25503== at 0x4E825EF: vfprintf (in /usr/lib64/libc-2.17.so)
==25503== by 0x4E89498: printf (in /usr/lib64/libc-2.17.so)
==25503== by 0x4005B7: main (in /home/ortalcohen/y/main)
==25503==
==25503== Conditional jump or move depends on uninitialised value(s)
==25503== at 0x4E80D1B: vfprintf (in /usr/lib64/libc-2.17.so)
==25503== by 0x4E89498: printf (in /usr/lib64/libc-2.17.so)
==25503== by 0x4005B7: main (in /home/ortalcohen/y/main)
==25503==
==25503== Conditional jump or move depends on uninitialised value(s)
==25503== at 0x4E80D9E: vfprintf (in /usr/lib64/libc-2.17.so)
==25503== by 0x4E89498: printf (in /usr/lib64/libc-2.17.so)
==25503== by 0x4005B7: main (in /home/ortalcohen/y/main)
==25503==
Should be error: 0
==25503==
==25503== HEAP SUMMARY:
==25503== in use at exit: 4 bytes in 1 blocks
==25503== total heap usage: 1 allocs, 0 frees, 4 bytes allocated
==25503==
==25503== 4 bytes in 1 blocks are definitely lost in loss record 1 of 1
==25503== at 0x4C29BC3: malloc (vg_replace_malloc.c:299)
==25503== by 0x40058E: main (in /home/ortalcohen/y/main)
==25503==
==25503== LEAK SUMMARY:
==25503== definitely lost: 4 bytes in 1 blocks
==25503== indirectly lost: 0 bytes in 0 blocks
==25503== possibly lost: 0 bytes in 0 blocks
==25503== still reachable: 0 bytes in 0 blocks
==25503== suppressed: 0 bytes in 0 blocks