Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
May 29th, 2013, 12:03 PM
 residentelvio
Contributing User

Join Date: May 2013
Posts: 56
Time spent in forums: 22 h 2 m 47 sec
Reputation 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;
}
}
}
salem disagrees: 15 posts, no code tags - you need to pay attention to presentation detail

#2
May 29th, 2013, 01:22 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,853
Time spent in forums: 3 Months 1 Day 2 h 51 m 40 sec
Reputation Power: 2199
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
Quote:

Yes, we did: http://forums.devshed.com/c-program...ray-945259.html

Last edited by dwise1_aol : May 29th, 2013 at 02:44 PM.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Segmentation Fault(core Dumped)