Here are some examples of things NOT to do.
Code:
int main ( ) {
int a;
free(&a); //!! oops, you didn't malloc this
}
==5014== Invalid free() / delete / delete[] / realloc()
==5014== at 0x4C2EDEB: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5014== by 0x4005B8: main (main.c:6)
==5014== Address 0xffefffd64 is on thread 1's stack
==5014== in frame #1, created by main (main.c:4)
Code:
int main ( ) {
char *p = malloc(10);
free(p); // OK
free(p); //!! oops, you did this already
}
==5022== Invalid free() / delete / delete[] / realloc()
==5022== at 0x4C2EDEB: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5022== by 0x400593: main (main.c:7)
==5022== Address 0x5203040 is 0 bytes inside a block of size 10 free'd
==5022== at 0x4C2EDEB: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5022== by 0x400587: main (main.c:6)
Code:
int main ( ) {
char *p = malloc(10);
for ( int i = 0 ; i < 10 ; i++ )
*p++ = '\0';
free(p); //!! oops, you messed with the pointer to the START of the memory
}
==5032== Invalid free() / delete / delete[] / realloc()
==5032== at 0x4C2EDEB: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5032== by 0x4005A9: main (main.c:8)
==5032== Address 0x520304a is 0 bytes after a block of size 10 alloc'd
==5032== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5032== by 0x400577: main (main.c:5)
Code:
int main ( ) {
char *p = malloc(10);
free(p);
//!! oops, you already freed the memory
for ( int i = 0 ; i < 10 ; i++ ) {
p[i] = '\0';
}
}
==5047== Invalid write of size 1
==5047== at 0x40059E: main (main.c:9)
==5047== Address 0x5203040 is 0 bytes inside a block of size 10 free'd
==5047== at 0x4C2EDEB: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5047== by 0x400587: main (main.c:6)
And in C++
Code:
int main ( ) {
char *p = new char; // just one char
delete [] p; //!! oops, you didn't call new[]
}
==5079== Mismatched free() / delete / delete []
==5079== at 0x4C2F74B: operator delete[](void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5079== by 0x40076E: main (main.cpp:6)
==5079== Address 0x5ab5c80 is 0 bytes inside a block of size 1 alloc'd
==5079== at 0x4C2E0EF: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5079== by 0x400757: main (main.cpp:5)
Code:
int main ( ) {
char *p = new char[10]; // 10 chars
delete p; //!! oops, you called new[]
}
==5093== Mismatched free() / delete / delete []
==5093== at 0x4C2F24B: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5093== by 0x400767: main (main.cpp:6)
==5093== Address 0x5ab5c80 is 0 bytes inside a block of size 10 alloc'd
==5093== at 0x4C2E80F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5093== by 0x400757: main (main.cpp:5)
Code:
int main ( ) {
char *p = new char[10]; // 10 chars
free(p); //!! oops, you called new[]
}
==5107== Mismatched free() / delete / delete []
==5107== at 0x4C2EDEB: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5107== by 0x400767: main (main.cpp:7)
==5107== Address 0x5ab5c80 is 0 bytes inside a block of size 10 alloc'd
==5107== at 0x4C2E80F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5107== by 0x400757: main (main.cpp:6)
If you read the rest of the messages carefully, it will tell you which particular combination of malloc / free / new / new[] / delete / delete[] you've messed up.
> I unable to see the line no in the valgrind report.
Then compile with the -g flag to enable symbols.
Compare these two runs with different compilation flags.
Code:
$ gcc -Wall main.c
$ valgrind ./a.out
==4952== Memcheck, a memory error detector
==4952== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==4952== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==4952== Command: ./a.out
==4952==
==4952== Invalid write of size 1
==4952== at 0x40059E: main (in /home/sc/Documents/a.out)
==4952== Address 0x5203040 is 0 bytes inside a block of size 10 free'd
==4952== at 0x4C2EDEB: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4952== by 0x400587: main (in /home/sc/Documents/a.out)
==4952== Block was alloc'd at
==4952== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4952== by 0x400577: main (in /home/sc/Documents/a.out)
==4952==
==4952==
==4952== HEAP SUMMARY:
==4952== in use at exit: 0 bytes in 0 blocks
==4952== total heap usage: 1 allocs, 1 frees, 10 bytes allocated
==4952==
==4952== All heap blocks were freed -- no leaks are possible
==4952==
==4952== For counts of detected and suppressed errors, rerun with: -v
==4952== ERROR SUMMARY: 10 errors from 1 contexts (suppressed: 0 from 0)
$ gcc -Wall -g main.c
$ valgrind ./a.out
==4963== Memcheck, a memory error detector
==4963== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==4963== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==4963== Command: ./a.out
==4963==
==4963== Invalid write of size 1
==4963== at 0x40059E: main (main.c:8)
==4963== Address 0x5203040 is 0 bytes inside a block of size 10 free'd
==4963== at 0x4C2EDEB: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4963== by 0x400587: main (main.c:6)
==4963== Block was alloc'd at
==4963== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4963== by 0x400577: main (main.c:5)
==4963==
==4963==
==4963== HEAP SUMMARY:
==4963== in use at exit: 0 bytes in 0 blocks
==4963== total heap usage: 1 allocs, 1 frees, 10 bytes allocated
==4963==
==4963== All heap blocks were freed -- no leaks are possible
==4963==
==4963== For counts of detected and suppressed errors, rerun with: -v
==4963== ERROR SUMMARY: 10 errors from 1 contexts (suppressed: 0 from 0)
$