Problem (Access violation (Segmentation Fault))

Hello
My Code leads to Error: Access violation (Segmentation Fault)
it is a heat transfer problem which calculate temperature of a canal.
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

#include <iostream>
#include <cmath>
#include<cstdio>
using namespace std;

#define I 15
#define J 18

int main ()
{
	int i, j, ts;
	double T[I][J][2], delt=.001, L=1, delx=L*I/15/6;
	double K=3,cp=400.0, p=2150.0, alpha=K/(p*cp), T1=80, T2=20;
	double Fo=alpha*delt/delx/delx, h1=5, h2=100, Bi1=h1*delx/K, Bi2=h2*delx/K;
	FILE *fd;
	fd=fopen("cfd1.plt","w");
	if((fd=fopen("cfd1.plt","w"))==NULL)
	{
		cout<<"cannot open file"<<endl;
	}
	for(i=0;i<=15;i++)                          //Initial Value Definition
	{
		for(j=0;j<=(18/9);j++)
		{
			T[i][j][0]=20;
		} 
		for(j=(7*J/9);j<=(J);j++)
		{
			T[i][j][0]=20;
		}
	}	
	

	for(j=(J/9);j<=(7*J/9);j++)
	{
		for(i=0;i<=(2*I/15);i++)
		{
			T[i][j][0]=20;
		}
		for(i=(14*I/15);i<=(I);i++)
		{
			T[i][j][0]=20;
		}
	}
	
	for(ts=0;ts<1;ts++)                     //Solve for Different Times
	{	
		for(i=1;i<I;i++)
		{	
			for(j=1;j<(J/9);j++)
			{
				T[i][j][1]=T[i][j][0]+Fo*(T[i+1][j][0]-2*T[i][j][0]+T[i-1][j][0])+Fo*(T[i][j+1][0]-2*T[i][j][0]+T[i][j-1][0]);
			}
			for(j=(7*J/9+1);j<J;j++)
			{
				T[i][j][1]=T[i][j][0]+Fo*(T[i+1][j][0]-2*T[i][j][0]+T[i-1][j][0])+Fo*(T[i][j+1][0]-2*T[i][j][0]+T[i][j-1][0]);
			}
		}
		for(j=(J/9+1);j<(7*J/9);j++)
		{	
			for(i=1;(i<2*I/15);i++)
			{
				T[i][j][1]=T[i][j][0]+Fo*(T[i+1][j][0]-2*T[i][j][0]+T[i-1][j][0])+Fo*(T[i][j+1][0]-2*T[i][j][0]+T[i][j-1][0]);
			}
			for(i=14*I/15;(i<I);i++)
			{
				T[i][j][1]=T[i][j][0]+Fo*(T[i+1][j][0]-2*T[i][j][0]+T[i-1][j][0])+Fo*(T[i][j+1][0]-2*T[i][j][0]+T[i][j-1][0]);
			}
		}	

		
		for(i=1;i<2*I/15;i++)                   //Boundarie Between 4 Blocks
		{
			T[i][J/9][1]=T[i][J/9][0]+Fo*(T[i+1][J/9][0]-2*T[i][J/9][0]+T[i-1][J/9][0])+Fo*(T[i][J/9+1][0]-2*T[i][J/9][0]+T[i][J/9-1][0]);			
		}
		for(i=14*I/15+1;i<I;i++)
		{
			T[i][J/9][1]=T[i][J/9][0]+Fo*(T[i+1][J/9][0]-2*T[i][J/9][0]+T[i-1][J/9][0])+Fo*(T[i][J/9+1][0]-2*T[i][J/9][0]+T[i][J/9-1][0]);			
		}
		for(i=1;i<2*I/15;i++)
		{
			T[i][7*J/9][1]=T[i][7*J/9][0]+Fo*(T[i+1][7*J/9][0]-2*T[i][7*J/9][0]+T[i-1][7*J/9][0])+Fo*(T[i][7*J/9+1][0]-2*T[i][7*J/9][0]+T[i][7*J/9-1][0]);			
		}
		for(i=14*I/15+1;i<I;i++)
		{
			T[i][7*J/9][1]=T[i][7*J/9][0]+Fo*(T[i+1][7*J/9][0]-2*T[i][7*J/9][0]+T[i-1][7*J/9][0])+Fo*(T[i][7*J/9+1][0]-2*T[i][7*J/9][0]+T[i][7*J/9-1][0]);			
		}
		                                        //8 Internals & Externals Points
		T[2*I/15][J/9][1]=2/3*Fo*(T[2*I/15+1][J/9][0]+2*T[2*I/15-1][J/9][0]+2*T[2*I/15][J/9-1][0]+T[2*I/15][J/9+1][0]+2*Bi1*T1)+(1-4*Fo-4/3*Bi1*Fo)*T[2*I/15][J/9][0];
		T[14*I/15][J/9][1]=2/3*Fo*(T[14*I/15-1][J/9][0]+2*T[14*I/15+1][J/9][0]+2*T[14*I/15][J/9-1][0]+T[14*I/15][J/9+1][0]+2*Bi1*T1)+(1-4*Fo-4/3*Bi1*Fo)*T[14*I/15][J/9][0];
		T[2*I/15][7*J/9][1]=2/3*Fo*(T[2*I/15+1][7*J/9][0]+2*T[2*I/15-1][7*J/9][0]+2*T[2*I/15][7*J/9+1][0]+T[2*I/15][7*J/9-1][0]+2*Bi1*T1)+(1-4*Fo-4/3*Bi1*Fo)*T[2*I/15][7*J/9][0];
		T[14*I/15][7*J/9][1]=2/3*Fo*(T[14*I/15-1][7*J/9][0]+2*T[14*I/15+1][7*J/9][0]+2*T[14*I/15][7*J/9+1][0]+T[14*I/15][7*J/9-1][0]+2*Bi1*T1)+(1-4*Fo-4/3*Bi1*Fo)*T[14*I/15][7*J/9][0];

		T[I][J][1]=2*Fo*(T[I-1][J][0]+T[I][J-1][0]+2*Bi2*T2)+(1-4*Fo-4*Bi2*Fo)*T[I][J][0];		
		T[I][0][1]=2*Fo*(T[I-1][0][0]+T[I][1][0]+2*Bi2*T2)+(1-4*Fo-4*Bi2*Fo)*T[I][0][0];
		T[0][J][1]=2*Fo*(T[1][J][0]+T[0][J-1][0]+2*Bi2*T2)+(1-4*Fo-4*Bi2*Fo)*T[0][J][0];		
		T[0][0][1]=2*Fo*(T[1][0][0]+T[0][1][0]+2*Bi2*T2)+(1-4*Fo-4*Bi2*Fo)*T[0][0][0];		
		
		for(i=2*I/15+1;i<14*I/15;i++)           //2 Internal Horizontal Boundaries
		{
			T[i][J/9][1]=Fo*(2*T[i][J/9-1][0]+T[i-1][J/9][0]+T[i+1][J/9][0]+2*Bi1*T1)+(1-4*Fo-4*Bi1*Fo)*T[i][J/9][0];
			T[i][7*J/9][1]=Fo*(2*T[i][7*J/9+1][0]+T[i-1][7*J/9][0]+T[i+1][7*J/9][0]+2*Bi1*T1)+(1-4*Fo-4*Bi1*Fo)*T[i][7*J/9][0];
		}
		for(i=1;i<I;i++)                       //2 External Horizontal Boundaries
		{
			T[i][0][1]=Fo*(2*T[i][1][0]+T[i-1][0][0]+T[i+1][0][0]+2*Bi2*T2)+(1-4*Fo-4*Bi2*Fo)*T[i][0][0];
			T[i][J][1]=Fo*(2*T[i][J-1][0]+T[i+1][J][0]+T[i-1][J][0]+2*Bi2*T2)+(1-4*Fo-4*Bi2*Fo)*T[i][J][0];
		}
		for(j=J/9+1;j<7*J/9;j++)               //2 Internal Vertical Boundaries
		{
			T[2*I/15][j][1]=Fo*(2*T[2*I/15-1][j][0]+T[2*I/15][j+1][0]+T[2*I/15][j-1][0]+2*Bi1*T1)+(1-4*Fo-4*Bi1*Fo)*T[2*I/15][j][0];
			T[14*I/15][j][1]=Fo*(2*T[14*I/15+1][j][0]+T[14*I/15][j+1][0]+T[14*I/15][j-1][0]+2*Bi1*T1)+(1-4*Fo-4*Bi1*Fo)*T[14*I/15][j][0];
		}
		for(j=0;j<J;j++)                      //2 External Vertical Boundaries
		{
			T[0][j][1]=Fo*(2*T[1][j][0]+T[0][j+1][0]+T[0][j-1][0]+2*Bi2*T2)+(1-4*Fo-4*Bi2*Fo)*T[0][j][0];
			T[I][j][1]=Fo*(2*T[I-1][j][0]+T[I][j+1][0]+T[I][j-1][0]+2*Bi2*T2)+(1-4*Fo-4*Bi2*Fo)*T[I][j][0];
		}
	for(i=0;i<=I;i++)
		for(j=0;j<=J;j++)
		{
		T[i][j][0]=T[i][j][1];
		}
		
	}
	
	
	for(i=0;i<=I;i++)
	{
		for(j=0;j<=J;j++)
		{
			fprintf(fd,"%d\t%d\t%.12f\n",i,j,T[i][j][0]);
		}
	}
	fclose(fd);
	
return 0;
}		
			
			
			
closed account (S6k9GNh0)
holy crap
Segmentation Fault
T[2*I/15][J/9][1]=2/3*Fo*(T[2*I/15+1][J/9][0]+2*T[2*I/15-1][J/9][0]+2*T[2*I/15][J/9-1][0]+T[2*I/15][J/9+1][0]+2*Bi1*T1)+(1-4*Fo-4/3*Bi1*Fo)*T[2*I/15][J/9][0];
Big surprise, there.

You're opening the same file twice, by the way.
it is not file. it is 3-D array which its 3rd dimension is time and 1 is newer and 0 is older temperature
I think s/he's referring to:

fd=fopen("cfd1.plt","w");
if((fd=fopen("cfd1.plt","w"))==NULL)
No, i deactivate those but no change occured.
what should i do?
Segmentation fault: Occurs when the program tries to access or modify inexistent/unreachable data pointed to by a pointer of sorts. To fix that problem, find the pointer.

Heh, good luck with that...

-Albie
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
for(i=0;i<=15;i++)                          //Initial Value Definition
	{
		for(j=0;j<=(18/9);j++)
		{
			T[i][j][0]=20;
		} 
		for(j=(7*J/9);j<=(J);j++)
		{
			T[i][j][0]=20;
		}
	}	
	

	for(j=(J/9);j<=(7*J/9);j++)
	{
		for(i=0;i<=(2*I/15);i++)
		{
			T[i][j][0]=20;
		}
		for(i=(14*I/15);i<=(I);i++)
		{
			T[i][j][0]=20;
		}


I think it might be with these for loops.
no, it is defining of matrix and no loop is seen.
???
closed account (S6k9GNh0)
Not going to lie. Why don't you use a debugger so you can find the exact line it crashes on.
Topic archived. No new replies allowed.