-1

I have been writing a program for multi-element diffusion. Because the method I am using is poorly optimized in MatLab, I have been programming in C, as that is fast for my purpose. I am, however, much more an engineer than a programmer. The model consists of many functions and each of those functions I have tested separately for expected inputs. However, now that everything is together, my program gets stuck. Most likely in one of the (many) while loops. If I can find which loop it gets stuck in, I can see if it gets a wrong input, or if I made a mistake in the loop itself that I missed during testing. If it were simply looping a few times, I could add a print statement of sorts in each loop, but since it iterates a few million to more than a billion times, that won't work. And if I try to run it with just a few hundred iterations, the problem doesn't occur. I was hoping that in the IDE there is an option to see which function is currently being executed, but I can't find it in the one I am using (Pelles C).

Is there an option in Pelles C (or if not there in another IDE) that shows which function is currently active? Or is there a different way to find where it gets stuck? I have been trying to get the debugger to tell me where it is stuck, but even though it gives me a lot of information about things I have no clue about, it doesn't seem to tell me what I want to know.

S. Hesam
  • 5,266
  • 3
  • 37
  • 59
Kvaestr
  • 77
  • 6
  • 4
    Let it run until it gets stuck, then attach to it using `gdb`. Backtrace will show you where you're stuck. – TCvD Dec 21 '20 at 09:07
  • As far as I can tell from a search on the internet, gdb is unix only. Since I am using Windows, I was hoping there is an alternative for it. – Kvaestr Dec 21 '20 at 09:19
  • 1
    There is: `WinDbg` – TCvD Dec 21 '20 at 09:22
  • Are you allowed to use [GCC](http://gcc.gnu.org/), perhaps on [Debian](http://debian.org/)? Then [invoke it](https://gcc.gnu.org/onlinedocs/gcc/Invoking-GCC.html) with all warnings and debug info: `gcc -Wall -Wextra -g` and use later [GDB](https://www.gnu.org/software/gdb/). **Take inspiration from *existing* open source software** (e.g. [GNU make](https://www.gnu.org/software/make/) and many others on [github](https://github.com/), http://savannah.gnu.org/ etc...) – Basile Starynkevitch Dec 21 '20 at 09:39
  • Actually, if you are allowed to do so, consider installing [Debian](http://debian.org/) or [Ubuntu](http://ubuntu.com/) on your laptop. I believe they are much more developer friendly (than Microsoft Windows): see http://linuxfromscratch.org/ – Basile Starynkevitch Dec 21 '20 at 09:44
  • I am going to see if linux can work for me. In my work environment we use the full microsoft suite to communicate, share files etc, so that works a lot better under windows. – Kvaestr Dec 21 '20 at 10:51

1 Answers1

1

Compile your program with -g flag and run it using gdb`

gdb ./test

whenever it gets stuck, run 'where' or 'bt' in gdb's command line, to find the exact line where it gets stuck. Also put -Wall flag, it will show all warnings.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
Sam______
  • 114
  • 7