简体   繁体   中英

Keeping a c program running

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

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.

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. eg 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM