Segmentation Fault in Function

Hi guys,

I am performing a function that calculates the number of zones grouped into an array of pixels in a CCD.

The point is that the function sometimes gives an error "segmentation fault" and sometimes not. But always correct results in the last "cout". This is very strange because the same values ​​with the function aborts and sometimes not, but always get a correct result for the number of zones. Also when I run the entire program slowly, the program works well Why is this? How I can fix it?

I know what is "segmetation fault", the function is designed to not access reserved memory areas, and I used a debbuger, and this is even more curious, because when I use debbuger never gives segmentation fault error and function runs perfectly.

Informatics someone can help a Spanish physicist? im running CodeBlocks in Ubuntu

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
int CalcularZonas (Pixel CCD[X][Y],int& corte_sat,int z)
{
    class buf
    {
        public:
        int k;
        int l;
    };

    int i,j,b,p,m;
    z=0;
    b=0;
    buf buffer[F];

    for(m=0;m<F;m++)
    {
        buffer[m].k=-1;
        buffer[m].l=-1;
    }



    for(i=0;i<X;i++)
    {
        for(j=0;j<Y;j++)
        {


            if(CCD[i][j].red>corte_sat && CCD[i][j].pas==false && CCD[i][j].prepas==false)
            {
                z++;
                b=0;
                m=0;
                CCD[i][j].zona=z;
                CCD[i][j].pas=true;

                if(CCD[i+1][j].red>corte_sat && CCD[i+1][j].prepas==false && CCD[i+1][j].pas==false && i+1<X)
                {
                    buffer[b].k=i+1;
                    buffer[b].l=j;
                    b++;
                    buffer[b].k=-1;
                    buffer[b].l=-1;
                    CCD[i+1][j].prepas=true;
                }else CCD[i+1][j].pas=true;
                if(CCD[i][j+1].red>corte_sat && CCD[i][j+1].prepas==false && CCD[i][j+1].pas==false && j+1<Y)
                {
                    buffer[b].k=i;
                    buffer[b].l=j+1;
                    b++;
                    buffer[b].k=-1;
                    buffer[b].l=-1;
                    CCD[i][j+1].prepas=true;
                }else CCD[i][j+1].pas=true;
                if(CCD[i-1][j].red>corte_sat && CCD[i-1][j].prepas==false && CCD[i-1][j].pas==false && 0<i-1)
                {
                    buffer[b].k=i-1;
                    buffer[b].l=j;
                    b++;
                    buffer[b].k=-1;
                    buffer[b].l=-1;
                    CCD[i-1][j].prepas=true;
                }else CCD[i-1][j].pas=true;
                if(CCD[i][j-1].red>corte_sat && CCD[i][j-1].prepas==false && CCD[i][j-1].pas==false && 0<j-1)
                {
                    buffer[b].k=i;
                    buffer[b].l=j-1;
                    b++;
                    buffer[b].k=-1;
                    buffer[b].l=-1;
                    CCD[i][j-1].prepas=true;
                }else CCD[i][j-1].pas=true;
                if(CCD[i+1][j+1].red>corte_sat && CCD[i+1][j+1].prepas==false && CCD[i+1][j+1].pas==false && i+1<X && j+1<Y)
                {
                    buffer[b].k=i+1;
                    buffer[b].l=j+1;
                    b++;
                    buffer[b].k=-1;
                    buffer[b].l=-1;
                    CCD[i+1][j+1].prepas=true;
                }else CCD[i+1][j+1].pas=true;
                if(CCD[i+1][j-1].red>corte_sat && CCD[i+1][j-1].prepas==false && CCD[i+1][j-1].pas==false && i+1<X && 0<j-1)
                {
                    buffer[b].k=i+1;
                    buffer[b].l=j-1;
                    b++;
                    buffer[b].k=-1;
                    buffer[b].l=-1;
                    CCD[i+1][j-1].prepas=true;
                }else CCD[i+1][j-1].pas=true;
                if(CCD[i-1][j+1].red>corte_sat && CCD[i-1][j+1].prepas==false && CCD[i-1][j+1].pas==false && j+1<Y && 0<i-1)
                {
                    buffer[b].k=i-1;
                    buffer[b].l=j+1;
                    b++;
                    buffer[b].k=-1;
                    buffer[b].l=-1;
                    CCD[i-1][j+1].prepas=true;
                }else CCD[i-1][j+1].pas=true;
                if(CCD[i-1][j-1].red>corte_sat && CCD[i-1][j-1].prepas==false && CCD[i-1][j-1].pas==false && 0<i-1 && 0<j-1)
                {
                    buffer[b].k=i-1;
                    buffer[b].l=j-1;
                    b++;
                    buffer[b].k=-1;
                    buffer[b].l=-1;
                    CCD[i-1][j-1].prepas=true;
                }else CCD[i-1][j-1].pas=true;

                p=-1;


                do {
                    p++;
                    CCD[buffer[p].k][buffer[p].l].pas=true;
                    CCD[buffer[p].k][buffer[p].l].zona=z;

                    buffer[p].k=-1;
                    buffer[p].l=-1;


                    if((CCD[(buffer[1+p].k)+1][buffer[1+p].l].red>corte_sat) && (CCD[(buffer[1+p].k+1)][buffer[1+p].l].prepas==false) 
&& (CCD[(buffer[1+p].k+1)][buffer[1+p].l].pas==false) && ((buffer[1+p].k)+1)<X)
                    {
                        buffer[b].k=buffer[1+p].k+1;
                        buffer[b].l=buffer[1+p].l;
                        b++;
                        buffer[b].k=-1;
                        buffer[b].l=-1;
                        CCD[(buffer[1+p].k)+1][buffer[1+p].l].pas=true;
                    }
                    if((CCD[(buffer[1+p].k)][buffer[1+p].l+1].red>corte_sat) && (CCD[(buffer[1+p].k)][buffer[1+p].l+1].prepas==false)
 && (CCD[(buffer[1+p].k)][buffer[1+p].l+1].pas==false) && ((buffer[1+p].l)+1)<Y)
                    {
                        buffer[b].k=buffer[1+p].k;
                        buffer[b].l=buffer[1+p].l+1;
                        b++;
                        buffer[b].k=-1;
                        buffer[b].l=-1;
                        CCD[(buffer[1+p].k)][buffer[1+p].l+1].pas=true;
                    }
                    if((CCD[(buffer[1+p].k)-1][buffer[1+p].l].red>corte_sat) && (CCD[(buffer[1+p].k)-1][buffer[1+p].l].prepas==false) 
&& (CCD[(buffer[1+p].k)-1][buffer[1+p].l].pas==false) && 0<((buffer[1+p].k)-1))
                    {
                        buffer[b].k=buffer[1+p].k-1;
                        buffer[b].l=buffer[1+p].l;
                        b++;
                        buffer[b].k=-1;
                        buffer[b].l=-1;
                        CCD[(buffer[1+p].k)-1][buffer[1+p].l].pas=true;
                    }
                    if((CCD[(buffer[1+p].k)][buffer[1+p].l-1].red>corte_sat) && (CCD[(buffer[1+p].k)][buffer[1+p].l-1].prepas==false) 
&& (CCD[(buffer[1+p].k)][buffer[1+p].l-1].pas==false) && 0<((buffer[1+p].l)-1))
                    {
                        buffer[b].k=buffer[1+p].k;
                        buffer[b].l=buffer[1+p].l-1;
                        b++;
                        buffer[b].k=-1;
                        buffer[b].l=-1;
                        CCD[(buffer[1+p].k)][buffer[1+p].l-1].pas=true;
                    }
                    if((CCD[(buffer[1+p].k)+1][(buffer[1+p].l)+1].red>corte_sat) && (CCD[(buffer[1+p].k)+1][(buffer[1+p].l)+1].prepas==false) 
&& (CCD[(buffer[1+p].k)+1][(buffer[1+p].l)+1].pas==false) && ((buffer[1+p].k)+1)<X && ((buffer[1+p].l)+1)<Y)
                    {
                        buffer[b].k=buffer[1+p].k+1;
                        buffer[b].l=buffer[1+p].l+1;
                        b++;
                        buffer[b].k=-1;
                        buffer[b].l=-1;
                        CCD[(buffer[1+p].k)+1][(buffer[1+p].l)+1].pas=true;
                    }
                    .
                    .
                    .

                } while((buffer[1+p].k)!=-1);
            }
            else
            {
               CCD[i][j].pas=true;
            }

        }
    }
cout<<z<<endl;
return z;
}
Take line 37:

if(CCD[i+1][j].red>corte_sat && CCD[i+1][j].prepas==false && CCD[i+1][j].pas==false && i+1<X)


When i == X-1, CCD[i+1] access memory that you have no business accessing when j is Y-1. Just accessing this memory is theoretically enough to cause a segmentation fault, but it is more likely caused by the accompanying else body.

Note that the if statement above will always return false when i is equal to X-1, because of the last condition in the if

The else:

else CCD[i+1][j].pas=true;

You have written to memory which you should not be accessing, and sooner or later it will cause problems.

I use this one specific case as an example, but the problem persists in many places within your code.
Topic archived. No new replies allowed.