r/learnprogramming • • 1d ago

PROTIP

If your C program "works sometimes and crashes other times," it's almost always undefined behavior — uninitialized variable, off-by-one on an array, or using memory after free. Run it with valgrind, it literally tells you the line. Changed my life in first year.

11 Upvotes

4 comments sorted by

4

u/TheOddHurricane 1d ago

Valgrind was one of those tools where I just sat there staring at the output thinking about all the hours I'd wasted before knowing it existed. Feels like cheating honestly.

2

u/lanternkettle 1d ago

Compile with -g first, then valgrind shows file and line numbers instead of addresses

1

u/MyNameIsRichardCS54 1d ago

Yeah, valgrind is awesome!

1

u/work_stuff_123 1d ago

Can confirm, my C/C++ experience got significantly better after I discovered valgrind.