r/learnprogramming • • 8d 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.

13 Upvotes

5 comments sorted by

4

u/TheOddHurricane 8d 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 8d ago

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

1

u/MyNameIsRichardCS54 8d ago

Yeah, valgrind is awesome!

1

u/work_stuff_123 8d ago

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

1

u/pjf_cpp 4d ago

Thanks.

Don't rely on Valgrind to catch all errors though.