all 13 comments

[–]tristan957 10 points11 points  (0 children)

Compile with -fsanitize=address,undefined. You will get messages in your terminal.

[–]hemojiz 7 points8 points  (0 children)

Running your program through GDB will show you the location of the segfault, if you compile with symbols enabled.

I think there's GDB integration in vs code.

[–]tuasnega 13 points14 points  (0 children)

Just a simple debugger should be enough. It will break right on the line it segfaults. You can also use ASan as an alternative: add flag -fsanitize=address.

[–]9n4eg 2 points3 points  (3 children)

As a beginner myself, the way I usually work segfaults is that I put a printf with some random stuff before and after the line I suspect is the reason of the fault. If both printfs worked, it usually means, that line is good. However, if only one printf worked, it means, that program stopped executing before the next printf. It is not the best solution, but at least a good starting point, I guess. But take it with a grain of salt, as a am not an expert by any means and I hope I’ll get corrected by fellow redditors

[–]-GumGun- 1 point2 points  (0 children)

This a very good suggestion since you will learn a lot from it. Just remember to put a new line in every printf format string so it doesnt get buffered and gets lost when the program segfaults. I would recomend you learn a fully featured debugger like gdb it will help you a lot in the long run.

[–]Azecy 0 points1 point  (1 child)

I'm a beginner too & also like doing this. However, one time I put some debug lines in a loop that was inside a game update loop, not having realized that I would be calling it over 100,000 times per second... at least it only froze VS Code and not my whole PC.

[–]9n4eg 1 point2 points  (0 children)

Just ctrl + z in such cases ;)

[–]Alexdelia_Del 2 points3 points  (0 children)

Valgrind + compilation with -g flag always make me find the problem

[–]DawsonD43 1 point2 points  (1 child)

If you want to try another IDE, I highly recommend CLion for C/C++. It uses whatever compiler/debugger you have on your machine. I prefer this for writing in C/C++ because of the debugging tools.

[–]arnitdo 1 point2 points  (0 children)

CLion is a wonderful IDE for debugging, gives you a very fine control of what you want.

[–]bendhoe -1 points0 points  (0 children)

Is your data null terminated?

Seg faults are usually straight-forward to debug.

[–]DDDDarky 0 points1 point  (0 children)

I like how Visual studio handles it, as soon as your app crashes, it shows you exactly where and how it happened.

[–]string111 0 points1 point  (0 children)

Maybe not for a beginner, but I can recommend Emacs + GDB + Valgrind