0Pricing
C Academy · Lesson

Invalid Access

Out-of-bounds and uninitialized.

Beyond Leaks

Leaks waste memory, but invalid access bugs corrupt it. memcheck catches several kinds:

  • Reading or writing past the end of a buffer
  • Using memory after free
  • Reading uninitialized values
  • Reading or writing through a bad pointer

These cause the most dangerous, hardest-to-reproduce crashes.

Out-of-Bounds Write

This allocates room for 5 ints but writes to index 5, the sixth slot.

Index 5 is one element past the end. memcheck reports an Invalid write of size 4 at this line.

#include <stdlib.h>

int main(void) {
    int *a = malloc(5 * sizeof(int));
    a[5] = 99; /* valid indices are 0..4 */
    free(a);
    return 0;
}

All lessons in this course

  1. Why Valgrind
  2. Detecting Leaks
  3. Invalid Access
  4. Reading Reports
← Back to C Academy