1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include <stdio.h> int main() { char *p; // Allocation #1 of 19 bytes p = (char *) malloc(13); // Allocation #2 of 12 bytes p = (char *) malloc(11); free(p); // Allocation #3 of 16 bytes p = (char *) malloc(16); return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | ==7279== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al. ==7295== Memcheck, a memory error detector ==7295== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al. ==7295== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info ==7295== Command: ./a ==7295== ==7295== ==7295== HEAP SUMMARY: ==7295== in use at exit: 29 bytes in 2 blocks ==7295== total heap usage: 3 allocs, 1 frees, 40 bytes allocated ==7295== ==7295== 13 bytes in 1 blocks are definitely lost in loss record 1 of 2 ==7295== at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so) ==7295== by 0x8048428: main (in /home/gradient/Escritorio/ValgrindErrors/a) ==7295== ==7295== 16 bytes in 1 blocks are definitely lost in loss record 2 of 2 ==7295== at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so) ==7295== by 0x8048454: main (in /home/gradient/Escritorio/ValgrindErrors/a) ==7295== ==7295== LEAK SUMMARY: ==7295== definitely lost: 29 bytes in 2 blocks ==7295== indirectly lost: 0 bytes in 0 blocks ==7295== possibly lost: 0 bytes in 0 blocks ==7295== still reachable: 0 bytes in 0 blocks ==7295== suppressed: 0 bytes in 0 blocks ==7295== ==7295== For counts of detected and suppressed errors, rerun with: -v ==7295== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0) |
Mira el error error Allocation #1 (13 byte leak) y responde.
Mira el error error Allocation #3 (16 byte leak) y responde.
Observa el siguiente código. ¿Cuál es el error que resultará si compilamos con Valgrind?
1 2 3 4 5 6 | #include <stdio.h> int main() { int x; printf ("x = %d\n", x); } |