Valgrind reports too many mallocs

Consider this code:

int main(int argc, char const *argv[])
{
    char *string = NULL;
    string = malloc(sizeof(char) * 30);
    free(string);
    return 0;
}

I am a malloc a char pointer, after which I will free it. Now consider the output of valgrind:

==58317== Memcheck, a memory error detector
==58317== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==58317== Using Valgrind-3.11.0.SVN and LibVEX; rerun with -h for copyright info
==58317== Command: ./a.out
==58317== 
==58317== 
==58317== HEAP SUMMARY:
==58317==     in use at exit: 34,941 bytes in 424 blocks
==58317==   total heap usage: 505 allocs, 81 frees, 41,099 bytes allocated
==58317== 
==58317== LEAK SUMMARY:
==58317==    definitely lost: 0 bytes in 0 blocks
==58317==    indirectly lost: 0 bytes in 0 blocks
==58317==      possibly lost: 0 bytes in 0 blocks
==58317==    still reachable: 0 bytes in 0 blocks
==58317==         suppressed: 34,941 bytes in 424 blocks
==58317== 
==58317== For counts of detected and suppressed errors, rerun with: -v
==58317== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

How can I have many mallocs and frees?

Edit: This is what I get when I start with valgrind --leak-check=yes --gen-suppressions=all ./a.out, I am trying to create a supp file.

==60943== Memcheck, a memory error detector
==60943== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==60943== Using Valgrind-3.11.0.SVN and LibVEX; rerun with -h for copyright info
==60943== Command: ./a.out
==60943== 
==60943== 
==60943== HEAP SUMMARY:
==60943==     in use at exit: 34,941 bytes in 424 blocks
==60943==   total heap usage: 505 allocs, 81 frees, 41,099 bytes allocated
==60943== 
==60943== LEAK SUMMARY:
==60943==    definitely lost: 0 bytes in 0 blocks
==60943==    indirectly lost: 0 bytes in 0 blocks
==60943==      possibly lost: 0 bytes in 0 blocks
==60943==    still reachable: 0 bytes in 0 blocks
==60943==         suppressed: 34,941 bytes in 424 blocks
==60943== 
==60943== For counts of detected and suppressed errors, rerun with: -v
==60943== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 15 from 15)
+4
source share
1 answer

These blocks were allocated (and some of them were freed) using the system library that was associated with your executable.

Valgrind has a default suppression file that suppresses leaks in system libraries, which you can see further in the results:

==58317==         suppressed: 34,941 bytes in 424 blocks

, , -v.

+6

All Articles