I am testing valgrind
and have this small C program that leaks 4 bytes:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int* x = malloc(sizeof(int));
printf( "Address: %p\n", x);
return 0;
}
I compile it with: gcc -g -o leak leak.c
, and run it:
$ leak
Address: 0x55a72e303260
$ leak
Address: 0x55f370273260
So it shows two different addresses for two separate runs. However if I run it under valgrind it always shows the same address: 0x4a66040
:
$ valgrind --leak-check=full --show-leak-kinds=all leak
==8186== Memcheck, a memory error detector
==8186== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==8186== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info
==8186== Command: leak
==8186==
Address: 0x4a66040
==8186==
==8186== HEAP SUMMARY:
==8186== in use at exit: 4 bytes in 1 blocks
==8186== total heap usage: 2 allocs, 1 frees, 1,028 bytes allocated
==8186==
==8186== 4 bytes in 1 blocks are definitely lost in loss record 1 of 1
==8186== at 0x483874F: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==8186== by 0x109156: main (leak.c:6)
==8186==
==8186== LEAK SUMMARY:
==8186== definitely lost: 4 bytes in 1 blocks
==8186== indirectly lost: 0 bytes in 0 blocks
==8186== possibly lost: 0 bytes in 0 blocks
==8186== still reachable: 0 bytes in 0 blocks
==8186== suppressed: 0 bytes in 0 blocks
==8186==
==8186== For counts of detected and suppressed errors, rerun with: -v
==8186== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Why is this so? And is it possible for valgrind
to show the real address of the memory?