Tsallaka zuwa abun ciki
Justice Tech School

Common Memory Bugs

Typical bugs: buffer overflow, use after free, double free, memory leaks, uninitialised variables and dangling pointers. Tools such as valgrind and -fsanitize=address find them.

Example
#include <stdlib.h>
#include <string.h>

int main(void) {
    char *p = malloc(4);
    strcpy(p, "too long");   // buffer overflow
    free(p);
    free(p);                 // double free
    return 0;
}
// gcc -g -fsanitize=address bug.c && ./a.out

Exercise

Detect memory errors.

gcc -fsanitize= x.c

Quiz Test

What is a memory leak?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.