0

I have developed two versions for solving knapsack problem. One allocates memory for N*C matrix while other allocates memory for (N+1)*(C+1) matrix. Maximum memory bandwidth is 8 GB. Both of my programs run successfully for certain input files and does not exceed memory bandwidth.

For one of the input files which exceeds memory bandwidth of 8 GB, it shows segmentation error. But while debugging program with gdb , it shows : for both versions:

Reading symbols from knapSeq1...(no debugging symbols found)...done.

But when I try to run the programs using

gdb run knapSeq1

It shows the following error:

for version 1:

The number of objects is 235638231400, and the capacity is 1. [ERROR] : Failed to allocate memory for weights/profits. [Inferior 1 (process 19432) exited with code 01]

with increase in number of objects in input file, process xxxx changes.

for version 2: it shows like this: - ( for one input file with which the program exceeds memory bandwidth) and shows segmentation error

The number of objects is -584969880, and the capacity is 1. Program received signal SIGSEGV, Segmentation fault. 0x00000036dd8894de in memset () from /lib64/libc.so.6

for other files with which it runs successfully, the output of gdb is same as above.

My questions are:

(1) if for version 1 , the program runs successfully ( gives correct output), why the gdb showing such error?
and for version 2, even if program runs successfully ( giving correct output) and does not show segmentation error, why the gdb showing such error?

(2) How can we correct the program so that it will never show segmentation error?

In both programs , I am allocating memory :

table = (int*) malloc ((N+1)*(C+1)*sizeof(int*));
table= (int*)malloc((N)*(C)*sizeof(int*));
qaphla
  • 4,707
  • 3
  • 20
  • 31
  • 4
    Apart from using an incorrect forma specifier to match the data type of your magnitude counter, there isn't nearly enough info in this to analyze your issue. I have a suspicion running this under Valgrind will be... educational. – WhozCraig Jul 23 '14 at 16:53
  • Actually in my knapsack program, N is number of objects ( data type: int) which forms number of rows in matrix, while C is capacity ( data type: int) which forms number of columns in matrix. I can save matrix N*C / (N+1)*(C+1) until it exceeds 8 GB. Whenever it exceeds 8GB value, it shows "segmentation error". Using gdb , I am trying to find out the cause. Any suggestions to correct this error? so that the program will never fail. – user3381449 Jul 23 '14 at 17:03
  • 1
    Try checking the return value from malloc to see if == NULL. There are various limits on program size, ranging from per process limits (see what `ulimit -d` says) to system wide limits (run `top` and add up the Mem and Swap numbers.) When you exceed the limit, malloc (and other things) will fail. – Mark Plotnick Jul 23 '14 at 17:55
  • You may also want to compile your program with debugging information, so that you get a full stack trace on crash (see http://stackoverflow.com/questions/89603/how-does-the-debugging-option-g-change-the-binary-executable) (that's probably why GDB says "no debugging symbols found") – Raphaël Saint-Pierre Jul 23 '14 at 20:25
  • @user3772710, The fact that, in version 2, you get a negative number, suggests that you are either using an int type too small for your needs or that something has gone wrong in the computation. – Raphaël Saint-Pierre Jul 23 '14 at 20:27
  • @user3772710 finally, where does the " Failed to allocate memory for weights/profits." message come from ? I guess you output it under certain circumstances, which are they ? – Raphaël Saint-Pierre Jul 23 '14 at 20:36
  • Thank u people for your comments. I solved error one using -g while compiling program with gcc. – user3381449 Jul 24 '14 at 18:19

0 Answers0