Skip to content

Commit

Permalink
for submission
Browse files Browse the repository at this point in the history
  • Loading branch information
maalvikabhat authored Mar 27, 2020
1 parent be77575 commit d037538
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions exercises/ex06/mem_errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,52 @@ int main()
int *free_twice = malloc(sizeof (int));
int *use_after_free = malloc(sizeof (int));
int *never_free = malloc(sizeof (int));
int array1[100];
// int array1[100];
int *array2 = malloc(100 * sizeof (int));

// valgrind does not bounds-check static arrays
read_element(array1, -1);
read_element(array1, 100);
// read_element(array1, -1);
// read_element(array1, 100);

// but it does bounds-check dynamic arrays
read_element(array2, -1);
read_element(array2, 100);
// // but it does bounds-check dynamic arrays
// read_element(array2, -1);
// read_element(array2, 99);

// and it catches use after free
// // and it catches use after free
free(use_after_free);
*use_after_free = 17;
// *use_after_free = 17;

// never_free is definitely lost
// // never_free is definitely lost
*never_free = 17;
free(never_free);

// the following line would generate a warning
// // the following line would generate a warning
// free(&never_allocated);

// but this one doesn't
free_anything(&never_allocated);
// // but this one doesn't
// free_anything(&never_allocated);

free(free_twice);
free(free_twice);
// free(free_twice);

free(array2);
return 0;
}

/*
And here's the final valgrind output:
==10461== Memcheck, a memory error detector
==10461== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==10461== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==10461== Command: ./mem
==10461==
==10461==
==10461== HEAP SUMMARY:
==10461== in use at exit: 0 bytes in 0 blocks
==10461== total heap usage: 4 allocs, 4 frees, 412 bytes allocated
==10461==
==10461== All heap blocks were freed -- no leaks are possible
==10461==
==10461== For counts of detected and suppressed errors, rerun with: -v
==10461== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
*/

0 comments on commit d037538

Please sign in to comment.