Valgrind

Logo

Measure the memory

vagrant@vagrant-ubuntu-trusty-64:~$ whatis free
free (1) - Display amount of free and used memory in the system
vagrant@vagrant-ubuntu-trusty-64:~$ free --si -h
total used free shared buffers cached
Mem: 501M 205M 295M 372K 18M 72M
-/+ buffers/cache: 114M 386M
Swap: 0B 0B 0B
vagrant@vagrant-ubuntu-trusty-64:~$ watch free --si -h
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int *p;
while(1)
{
p = malloc(128);
printf("%ld\n", (long)p);
}
return (0);
}

The HEAP

visual representation of a heap overflow()

What is Valgrind?

How to install Valgrind?

vagrant@vagrant-ubuntu-trusty-64:~$ sudo apt install valgrind -y
.
.
.
vagrant@vagrant-ubuntu-trusty-64:~$ valgrind --version
valgrind-3.10.1
#include <stdio.h>
#include <unistd.h>
int main(void)
{
printf("%ld, %s\n", (long)getpid());
return (0);
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
char *msg = malloc(10);
printf("%ld, %s\n", (long)getpid(), msg);
return (0);
}
valgrind show lost memory
diff with/without leak-ckeck=full
vagrant@vagrant-ubuntu-trusty-64:~$ cat .valgrindrc 
--leak-check=full
no error output
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(void)
{
char *msg = malloc(8);
strcpy(msg, "Holberton");
msg[0] = 'R';
printf("%ld, %s\n", (long)getpid(), msg);
free(msg);
return (0);
}

Configuration Files

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store