C/Wskaźniki: Różnice pomiędzy wersjami

Przejdź do nawigacji Przejdź do wyszukiwania
Dodane 1786 bajtów ,  7 lat temu
#include <stdlib.h>
voidint fmain()
{
int *ptr = (int *) malloc(sizeof(int));
/* Do some work */
return 0 ; /* Return without freeing ptr*/
}
</source>
 
Sprawdzamy za pomocą [[Programowanie w systemie UNIX/Valgrind|Valgrinda]]
 
<pre>
<noinclude>
valgrind --leak-check=full ./a.out
==3382== Memcheck, a memory error detector
==3382== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==3382== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==3382== Command: ./a.out
==3382==
==3382==
==3382== HEAP SUMMARY:
==3382== in use at exit: 4 bytes in 1 blocks
==3382== total heap usage: 1 allocs, 0 frees, 4 bytes allocated
==3382==
==3382== 4 bytes in 1 blocks are definitely lost in loss record 1 of 1
==3382== at 0x4C2A2DB: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3382== by 0x40053E: main (l.c:13)
==3382==
==3382== LEAK SUMMARY:
==3382== definitely lost: 4 bytes in 1 blocks
==3382== indirectly lost: 0 bytes in 0 blocks
==3382== possibly lost: 0 bytes in 0 blocks
==3382== still reachable: 0 bytes in 0 blocks
==3382== suppressed: 0 bytes in 0 blocks
==3382==
==3382== For counts of detected and suppressed errors, rerun with: -v
==3382== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)
</pre>
 
 
#include <stdlib.h>;
voidint fmain()
{
int *ptr = (int *) malloc(sizeof(int));
free(ptr);
return 0;
}
</source>
 
 
<pre>
 
valgrind --leak-check=full ./a.out
==3397== Memcheck, a memory error detector
==3397== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==3397== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==3397== Command: ./a.out
==3397==
==3397==
==3397== HEAP SUMMARY:
==3397== in use at exit: 0 bytes in 0 blocks
==3397== total heap usage: 1 allocs, 1 frees, 4 bytes allocated
==3397==
==3397== All heap blocks were freed -- no leaks are possible
==3397==
==3397== For counts of detected and suppressed errors, rerun with: -v
==3397== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
 
</pre>
 
 
6840

edycji

Menu nawigacyjne