Keeping a c program running

178 views Asked by At

After having successfully implemented the karatsuba algorithm, I decided to compare the time needed with the school algorithm. The program needs to test up to 32768 digits. Unfortunately, it stops at 8192 digits(the digits are stored in an array). When running it with gdb I get the output: Programme terminated with SIGKILL, Killed. So obviously I searched through the web and found out that(since I'm on Linux), the kernel automatically killed the program because it consumed too much of resources. So my question is: Is there a way to keep it running?

Thanks in advance for any response

2

There are 2 answers

1
AudioBubble On

The most probable cause is memory exhaustion. You can roughly test this hypothesis by running top on the terminal.

If this is the case, valgrind is your friend. Look very carefully at every place you call malloc in your program and ensure that you call free for each array afterwards.

0
Painted Black On

I see a number of things you should do before forcing Linux to keep your program running (if you could do that anyway).

  1. Watch out for memory leaks (see answer of jons34yp)
  2. Once all memory leaks resolved, check the declaration of your variables, every non used bit but allocated bit is one to many. If a byte is enough (unsigned char), don't use a short. If a short is enough, don't use a long. Same for float's and doubles. Also check eventual structs and unions for unused data.
  3. Also check your algorithm and the way you implement it. e.g. a sparse matrix can be represented in other ways than waisting entire array's.
  4. Keep in mind that C compilers use to align data fields. This means that after for instance, an array of 13 bytes, compilers tend to align the next bytes on an 32bit or 64bit boundary, leaving you with unused bytes in between. The same thing can happen within structs. So check your compilers alignment settings.

I hope this helps to find a solution.

Kind regards, PB