0

This is a rather general question.

If you have a programm with many many lines of code, let's say C++. Durring compilation everything runs fine, no warnings no errors. But during executing the programm suddenly freezes, which leads to a crash.

How does one solve this problem, if you have pretty much no information from where this could happening (could be loops, could be pointer, could be wrong initialization, could be ...).

Are there any techniques, or profilers that track the current line of the programm execution ?

user3085931
  • 1,757
  • 4
  • 29
  • 55
  • 5
    Voila! You've just asked the world about debuggers. – therainmaker Nov 19 '15 at 11:55
  • 2
    how comes you know how to use the `debugging` tag but you never heard about debugging? – 463035818_is_not_an_ai Nov 19 '15 at 11:56
  • 1
    If stack overflow put that "debugging" tag in there for you automatically then it is a candidate for the Turing test. If it didn't then your question is rhetorical. – Bathsheba Nov 19 '15 at 11:57
  • 2
    I'm voting to close this question as off-topic because the answer is contained within the question. – Bathsheba Nov 19 '15 at 11:58
  • I'm sorry but this ain't anything near to an answer, debugging is an infinit expression – user3085931 Nov 19 '15 at 11:59
  • @user3085931 I am not sorry ;), but debugging is not an infinit expression as you call it, it is a technique to track what is happening in your program line by line. And thats exactly what you are asking for. – 463035818_is_not_an_ai Nov 19 '15 at 12:06
  • That's because you are asking an infinite question. – Filburt Nov 19 '15 at 12:06
  • 1
    Look, just run it under gdb. (Turn off compiler optimization.) If it hangs long enough, just hit Ctrl-C while it's hung, and see what it's doing. If it crashes before you can do that, gdb will show you where it crashed. Alternately, you can step through the program, at each statement making sure it is doing the right thing. When it crashes you will know where. This is what programmers do. – Mike Dunlavey Nov 19 '15 at 18:36

1 Answers1

1

Your question is too broad, and there is no general answer. In general, the bug is yours (don't suspect at first the compiler or the implementation to be wrong, almost always you are wrong, not the system!).

First, read carefully about the Halting Problem and Undecidable Problem.

Then, be extremely cautious of undefined behavior (UB) in your code (not all of them give segmentation faults, see this). C++ (& C) code can have a lot of them. Some languages (Haskell, Scheme, Common Lisp....) are better specified and have much less UB.

Concretely,

  • enable all warnings and debug info in your compiler, so compile with g++ -Wall -Wextra -g if using GCC (or likewise with Clang/LLVM). Sometimes you'll be happy to use some sanitizers, e.g. compile with some -fsanitize= flags.

  • learn how to use the debugger (e.g. gdb), and also valgrind

  • learn much more about C++, since it is a difficult language.

  • understand and follow coding rules and guidelines (e.g. the rule of 5).

  • be curious and learn many other languages and concepts (so read SICP and learn Scheme).

You'll need ten years to learn programming, so be patient.

PS. My biased advice is to install Linux on your laptop.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547