1

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.

captain
  • 815
  • 14
  • 26

1 Answers1

6

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.

flolo
  • 15,148
  • 4
  • 32
  • 57
  • The variable m is logged, so it's quite small. The algorithm is giving me correct results now when I used new to allocate `C`. But the overlapping still happens with other arrays!!. Surprisingly I get a segmentation fault when I replace pow by 2< – captain Mar 26 '12 at 12:41
  • 1
    m is logged? In your posted coded you definitely allocate space only for m rows, but you access rows via i up to 2^m. Sure m is log from i, but then you have only allocated loged rows, but require all. That has definitely nothing do with the kind of allocation, wether it a static array, a malloced or a new one. If you just switch the kind of allocation, the bug will be still there, but instead of resulting in obvious errors (or clear segfaults) it is (at the moment) just better hidden. – flolo Mar 26 '12 at 12:48
  • Thanks for pointing that out. I could never have guessed it!! . It fixed the error :) – captain Mar 26 '12 at 12:53
  • I'm wondering why I did not get a segmentation fault. Is it because it used the next array's space? – captain Mar 27 '12 at 13:17