# Memory Debugging: ASan और Valgrind — C Programming Fundamentals: Pointers, Memory और Files

Source: https://www.geekswithgeeks.com/hi/c/c-memory-debugging

> अंदाज़ा लगाने के बजाय AddressSanitizer और Valgrind से buffer overflows, leaks और use-after-free bugs पकड़ें।

## AddressSanitizer

`-fsanitize=address` के साथ compile करें और program stack trace सहित out-of-bounds access, use-after-free और leaks बताएगा।

```bash
gcc -g -O1 -fsanitize=address,undefined -fno-omit-frame-pointer prog.c -o prog
./prog
# ERROR: AddressSanitizer: heap-buffer-overflow ... at prog.c:12
```

## Valgrind

Valgrind बिना बदले binary चलाता है और invalid reads व leaked blocks बताता है, पर यह धीमा है।

```bash
gcc -g prog.c -o prog
valgrind --leak-check=full ./prog
```

CI में tests को sanitizer के साथ चलाएँ। Sanitizer builds development के लिए हैं, production के लिए नहीं, क्योंकि वे programs को धीमा करते हैं और अधिक memory लेते हैं।
