I'm working on binary matrices. Two of my arrays are getting overlapped. (I checked the addresses).This happens only for few sizes of matrices. Hence I get wrong results. I tried using new to allocate arrays, but I get segmentation fault when I do. Is there a way I can avoid overlapping of memory? I'm using g++ compiler.
This is how I declared the arrays
bool A[size0][size0],B[size0][size0],C[size0][size0];
In the next step I initialize all of them. A and B are the operands and C will be holding the result.
I'm using a custom multiplication algorithm in the next stage. Here's a snippet
for(I=0;I<cnm;I++){
bool Tab[m][size];
bool Ctemp[size][size];
int count=0;
for(uint i=0;i<pow(2.0,m*1.0);i++){
for(uint j=0;j<n;j++){
Tab[i][j]=0; //Initialize
if(i==0){
Tab[i][j] = 0;
}
else{
int dec;
//h is calculated here
dec=gray_map[i-1]^gray_map[i]; //gray_map returns gray code
Tab[i][j] = Tab[i-1][j] ^ B[h][j];
}
....
....
}
}
.....
.....
//Rest of the code
As per my observation Tab
and C
are overlapped. I checked the memory addresses by printing them. They overlap at the sixth iteration of the second level for loop.(n=9, m=3, size=9, cnm=3). I have NOT used C
in between, I use it only in the outer loop.
C-Compliers dont let overlap arrays (except you tell them or they are really buggy).
Most times the reason for such errors are wrong pointer arithmetic or wrong array access.
Even when just long 3 secs at your code, I see, that something is wrong with your access:
you declared Tab[m][..]
but you got a loop iterating over i
from 0 to 2^m (btw using pow for calculating it, is not very good, use instead a left shift (<<)). And then you access Tab[i][...]
, so most times you access Tab at undeclared indexes.