Sobre Valgrind

/*
| example1.c
|
| Uso de Valgrind
| 
| If you use uninitialized memory, write off the end of an array, 
| or forget to free a pointer, Valgrind can detect it. 
| 
| We'll focus mainly on the memcheck tool for this tutorial as running 
| valgrind with the memcheck tool will allow us to check correct memory usage.
| 
| 
| Compilacion
| $ gcc example1.c -o example1 -g
| 
| Valgrind
| $ valgrind --tool=memcheck --leak-check=yes ./example1
|
| Opciones
| --tool=memcheck
| --leak-check=yes
| --show-reacheable=yes  There will be times when the --leak-check=yes option will not result in showing you all memory leaks. 
*                       To find absolutely every unpaired call to free or new, you'll need to use the --show-reachable=yes option. Its output is almost exactly the same, but it will show more unfreed memory. 
| 
| Salida de Valgrind

==5316== Memcheck, a memory error detector
==5316== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==5316== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==5316== Command: ./example1
==5316== 
==5316== 
==5316== HEAP SUMMARY:
==5316==     in use at exit: 100 bytes in 1 blocks
==5316==   total heap usage: 1 allocs, 0 frees, 100 bytes allocated                         [1]
==5316== 
==5316== 100 bytes in 1 blocks are definitely lost in loss record 1 of 1                    [2]
==5316==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==5316==    by 0x80483F8: main (example1.c:58)                                              [3]
==5316== 
==5316== LEAK SUMMARY:
==5316==    definitely lost: 100 bytes in 1 blocks                                          [4]
==5316==    indirectly lost: 0 bytes in 0 blocks
==5316==      possibly lost: 0 bytes in 0 blocks
==5316==    still reachable: 0 bytes in 0 blocks
==5316==         suppressed: 0 bytes in 0 blocks
==5316== 
==5316== For counts of detected and suppressed errors, rerun with: -v
==5316== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

| 
| [1] El numero de allocs no coincide con el numero de frees, por lo tanto, no se esta liberando
|     memoria correctamente.
| [2] Memoria perdida
| [3] Para conocer el numero de linea que causa el problema, se debe usar la opcion -g de gcc
| [4] Sumario de memoria perdida
| 
| 
| Fuente: http://www.cprogramming.com/debugging/valgrind.html
*/

#include <stdlib.h>

int main()
{
    char *x = malloc(100);
    return 0;
}
/*
| example2.c
|
| Uso de Valgrind
| 
| Valgrind can also find the use of invalid heap memory using the memcheck tool. 
| For instance, if you allocate an array with malloc or new and then try to 
| access a location past the end of the array:
| 
| char *x = malloc(10);
| x[10] = 'a';
|
| Compilacion
| $ gcc example2.c -o example2 -g
| 
| Valgrind
| $ valgrind --tool=memcheck --leak-check=yes ./example1
|
| Opciones
| --tool=memcheck
| --leak-check=yes
| --show-reachable=yes  There will be times when the --leak-check=yes option will not result in showing you all memory leaks. 
*                       To find absolutely every unpaired call to free or new, you'll need to use the --show-reachable=yes option. Its output is almost exactly the same, but it will show more unfreed memory. 
| 
| Salida de Valgrind
|
==5517== Invalid write of size 1                                    [1]
==5517==    at 0x8048404: main (example2.c:67)
==5517==  Address 0x4200032 is 0 bytes after a block of size 10 alloc'd
==5517==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==5517==    by 0x80483F8: main (example2.c:66)
|
| 
| [1] Error de escritura
| 
| 
| Fuente: http://www.cprogramming.com/debugging/valgrind.html
*/

#include <stdlib.h>

int main()
{
    char *x = malloc(10);
    x[10] = 'a';
    return 0;
}
/*
| example3.c
|
| Uso de Valgrind
| 
| Another type of operation that Valgrind will detect is the use of an uninitialized 
| value in a conditional statement. Although you should be in the habit of 
| initializing all variables that you create, Valgrind will help find those cases where you don't. 
| 
| Compilacion
| $ gcc example3.c -o example3 -g
| 
| Valgrind
| $ valgrind --tool=memcheck --leak-check=yes --show-reachable=yes  ./example3
|
| Opciones
| --tool=memcheck
| --leak-check=yes
| --show-reachable=yes  There will be times when the --leak-check=yes option will not result in showing you all memory leaks. 
*                       To find absolutely every unpaired call to free or new, you'll need to use the --show-reachable=yes option. Its output is almost exactly the same, but it will show more unfreed memory. 
| 
| Salida de Valgrind
|
==5661== Conditional jump or move depends on uninitialised value(s) [1]
==5661==    at 0x80483F2: main (example3.c:47)
|
| 
| [1] Uso de variable no inicializada
| 
| 
| Fuente: http://www.cprogramming.com/debugging/valgrind.html
*/

#include <stdlib.h>
#include <stdio.h>

int main()
{
    int x;
    
    if (x == 0)
        printf("x is zero");
    
    return 0;
}
/*
| example4.c
|
| Uso de Valgrind
| 
| Another type of operation that Valgrind will detect is the use of an uninitialized 
| value in a conditional statement. Although you should be in the habit of 
| initializing all variables that you create, Valgrind will help find those cases where you don't. 
| 
| Compilacion
| $ gcc example4.c -o example4 -g
| 
| Valgrind
| $ valgrind --tool=memcheck --leak-check=yes --show-reachable=yes  ./example4
|
| Opciones
| --tool=memcheck
| --leak-check=yes
| --show-reachable=yes  There will be times when the --leak-check=yes option will not result in showing you all memory leaks. 
*                       To find absolutely every unpaired call to free or new, you'll need to use the --show-reachable=yes option. Its output is almost exactly the same, but it will show more unfreed memory. 
| 
| Salida de Valgrind
|
==5733== Conditional jump or move depends on uninitialised value(s) [1]
==5733==    at 0x80483EE: foo (example4.c:39)
==5733==    by 0x8048413: main (example4.c:47)

|
| 
| [1] Traceback de la variable no inicializada
| 
| 
| Fuente: http://www.cprogramming.com/debugging/valgrind.html
*/

#include <stdlib.h>
#include <stdio.h>

void foo(int y)
{
    if (y == 0)
        printf("y is zero");
}


int main()
{
    int x;
    foo(x);
    
    return 0;
}
/*
| example5.c
|
| Uso de Valgrind
| Valgrind doesn't perform bounds checking on static arrays (allocated on the stack).
| So if you declare an array inside your function: 
| 
|   char x[10];
|   x[11] = 'a';
|
| Compilacion
| $ gcc example5.c -o example5 -g
| 
| Valgrind
| $ valgrind --tool=memcheck --leak-check=yes --show-reachable=yes  ./example5
|
| Opciones
| --tool=memcheck
| --leak-check=yes
| --show-reachable=yes  There will be times when the --leak-check=yes option will not result in showing you all memory leaks. 
*                       To find absolutely every unpaired call to free or new, you'll need to use the --show-reachable=yes option. Its output is almost exactly the same, but it will show more unfreed memory. 
| 
| Salida de Valgrind
|
==5733== Conditional jump or move depends on uninitialised value(s) [1]
==5733==    at 0x80483EE: foo (example4.c:39)
==5733==    by 0x8048413: main (example4.c:47)

|
| 
| [1] Traceback de la variable no inicializada
| 
| 
| Fuente: http://www.cprogramming.com/debugging/valgrind.html
*/

#include <stdlib.h>
#include <stdio.h>

int main()
{
    int x[10];
    //x[9] = 1;       // correcto
    x[11] = 1;       // correcto
    
    return 0;
}

Fuente: cprogramming.com

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s