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*));