#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    57
    Rep Power
    0

    Segmentation Fault(core Dumped)


    Hi, i m texting my function that has to shift elements of a bidimensional array. I need to use **filter.

    void ftshifting(double **filter,int rows,int cols);

    int main(){
    double **filter;
    int rows=2;
    int cols=5;

    for(int a=0;a<rows;a++){
    for (int b=0;b<cols;b++){

    filter[a][b]= (double)(a*2)+(b*3);
    printf("FOR a=%d , b=%d , value is %d/n",a,b,(int)filter[a][b]);

    }
    }
    printf("USING THE SHIFTING/n/n");

    ftshifting(filter,rows,cols);


    for(int a=0;a<rows;a++){
    for (int b=0;b<cols;b++){
    printf("FOR a=%d , b=%d , value is %d/n",a,b,(int)filter[a][b]);
    }
    }

    return 0;
    }

    void ftshifting(double **filter,int rows,int cols){


    int r2,c2,tmp1_3,tmp2_4;

    r2 = rows/2;
    c2 = cols/2;


    for (int i = 0; i<r2; i++){
    for (int k = 0; k<c2; k++){

    tmp1_3 = filter[i][k];
    filter[i][k] = filter[i+r2][k+c2];
    filter[i+r2][k+c2] = tmp1_3;

    tmp2_4 = filter[i+r2][k];
    filter[i+r2][k] = filter[i][k+c2];
    filter[i][k+c2] = tmp2_4;
    }
    }
    }

    Comments on this post

    • salem disagrees : 15 posts, no code tags - you need to pay attention to presentation detail
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,156
    Rep Power
    2222
    USE CODE TAGS!

    How many times have we told you that? What part of use code tags do you refuse to understand?

    Code:
    void ftshifting(double **filter,int rows,int cols);
    
    int main(){
        double **filter;
        int rows=2;
        int cols=5;
    
        for(int a=0;a<rows;a++){
            for (int b=0;b<cols;b++){
    
                filter[a][b]= (double)(a*2)+(b*3);
                printf("FOR a=%d , b=%d , value is %d/n",a,b,(int)filter[a][b]);
    
            }
        }
        printf("USING THE SHIFTING/n/n");
    
        ftshifting(filter,rows,cols);
    
    
        for(int a=0;a<rows;a++){
             for (int b=0;b<cols;b++){
                printf("FOR a=%d , b=%d , value is %d/n",a,b,(int)filter[a][b]);
             }
        }
    
        return 0;
    }
    
    void ftshifting(double **filter,int rows,int cols){
    
    
      int r2,c2,tmp1_3,tmp2_4;
    
      r2 = rows/2;
      c2 = cols/2;
    
    
      for (int i = 0; i<r2; i++){
         for (int k = 0; k<c2; k++){
    
              tmp1_3 = filter[i][k];
              filter[i][k] = filter[i+r2][k+c2];
              filter[i+r2][k+c2] = tmp1_3;
    
              tmp2_4 = filter[i+r2][k];
              filter[i+r2][k] = filter[i][k+c2];
              filter[i][k+c2] = tmp2_4;
         }
      }
    }
    filter is an uninitialized pointer. As such, it contains an garbage address which could be pointing anywhere. Would you take a loaded gun into a crowded room and point it in any random direction? Of course not, but that's what you're doing with this uninitialized pointer. You're ending up writing to a memory location that you have not been granted write access to. Like the police would to do you in the analogy, the operating system has taken you out of action with extreme prejudice for the sake and safety of everybody else.

    Before you can use filter, you need to initialize it to point to memory that you own and that you can write to safely. I have a feeling that we've talked about this before.

    PS
    Originally Posted by DWise1_aol
    I have a feeling that we've talked about this before.
    Yes, we did: http://forums.devshed.com/c-programm...ay-945259.html
    Last edited by dwise1_aol; May 29th, 2013 at 01:44 PM.

IMN logo majestic logo threadwatch logo seochat tools logo