问题描述
我有以下的code我的程序
I have the following code for my program
int main(void)
{
int i,size;
float vel_x[size],vel_y[size],vel_x0[size],vel_y0[size],rho[size],rho0[size];
float *ux, *vy, *ux0, *vy0;
float *r, *r0;
struct fdparam fdparam_1;
printf("Enter the number of grid points: \t");
scanf("%d", &fdparam_1.N);
printf("Enter the maximum number of iterations: \t");
scanf("%d", &fdparam_1.MAXIT);
printf("Enter the value for time domain: \t");
scanf("%f", &fdparam_1.t_domain);
printf("Enter the time step and density of the fluid: \t \t");
scanf("%f\t%f", &fdparam_1.Dt, &fdparam_1.dens);
printf("Enter the diffusion coefficient and viscosity: \t \t");
scanf("%f\t%f",&fdparam_1.diff, &fdparam_1.mu);
printf("The parameters are N=%d, MAXIT=%d, t_domain=%f, Dt=%f, diff=%e, mu=%e, dens=%f \n",fdparam_1.N, fdparam_1.MAXIT, fdparam_1.t_domain, fdparam_1.Dt, fdparam_1.diff, fdparam_1.mu, fdparam_1.dens);
size=(fdparam_1.N+2)*(fdparam_1.N+2);
printf("The size is %d \n",size );
r = (float*) calloc (size,sizeof(float));
r0 = (float*) calloc (size,sizeof(float));
ux = (float*) calloc (size,sizeof(float));
vy = (float*) calloc (size,sizeof(float));
ux0 = (float*)calloc (size,sizeof(float));
vy0 = (float*)calloc (size,sizeof(float));
var_init(fdparam_1.N,r0,ux0,vy0,fdparam_1.dens);
// t=0;
// Solver functions
// while (t<fdparam_1.t_domain){
velocity_solve(fdparam_1.N,ux,vy,ux0,vy0,fdparam_1.Dt,fdparam_1.mu,fdparam_1.MAXIT); //calculates ux and vy to be used in the density solver
density_solve(fdparam_1.N,r,r0,ux,vy,fdparam_1.Dt,fdparam_1.diff,fdparam_1.MAXIT); //uses ux and vy calculated from Navier Stokes in the velocity solver to calculate density r
// t+=fdparam_1.Dt
// }
}
//velocity solver function
void velocity_solve(int n, float *u, float *v, float *u0, float *v0, float dt, float m, int MAXITER)
{
int i,j;
add_source(n,u,u0,dt); add_source(n,v,v0,dt);
swap(u0,u); swap(v0,v);
diffusion(n,u,u0,dt,m,MAXITER); diffusion(n,v,v0,dt,m,MAXITER);
projection(n,u,v,u0,v0,MAXITER);
swap(u0,u); swap(v0,v);
advection(n,u,u0,u0,v0,dt); advection(n,v,v0,u0,v0,dt);
projection(n,u,v,u0,v0,MAXITER);
printf("Printing velocities now \n");
for (i=0; i<=n+1; i++){
for (j=0;j<=n+1;j++){
printf("%f \t %f \n",u[ix(i,j)],v[ix(i,j)]);
}
}
}
// density solver function
void density_solve(int n, float *x, float *x0, float *u, float *v, float dt, float diff, int MAXITER)
{
int i,j;
add_source(n,x,x0,dt);
swap(x0,x);
diffusion(n,x,x0,dt,diff,MAXITER);
swap(x0,x);
advection(n,x,x0,u,v,dt);
printf("Printing density now \n");
for (i=0;i<=n+1;i++){
for (j=0; j<=n+1;j++){
printf("%f \t",x[ix(i,j)]);
}
}
printf("\n");
}
在执行此code,我面临的主要问题是,我得到一个分段错误,当我尝试运行两种功能 velocity_solve
和 density_solve
在一起。这两种功能的正确执行,当他们单独运行,即当我评论 velocity_solve
, density_solve
运行良好,反之亦然
The main problem that I am facing while executing this code is that I get a segmentation fault when I try to run both the functions velocity_solve
and density_solve
together. The two functions are executed properly when they are run individually i.e. when I comment velocity_solve
, density_solve
runs fine and vice-versa.
我是pretty肯定没有错与正在使用的功能 velocity_solve
和 density_solve
例程因为无论是两个例程会给予个别输出。我怀疑有一些事情错了,当两种功能都应该互动,即 velocity_solve
的输出需要在 density_solve $ C被使用$ C>。
I am pretty sure there is nothing wrong with the functions being used in the velocity_solve
and density_solve
routines because neither of the two routines would give individual outputs. I suspect there is something going wrong when the two functions are supposed to interact, i.e the output of velocity_solve
needs to be used in density_solve
.
我试过初始化大小可变,但随后并没有有所作为的。但后来我意识到,我不使用被定义所以我评论他们走出阵列,而不是我用,我已经分配的内存的指针,那么为什么它仍然给分段错误,当我尝试调用两种功能在一起的主要功能?
I tried initializing the size variable but then it doesn't make a difference at all. But then I realized that I do not use the arrays that are defined so I commented them out, instead I use the pointers for which I have already allocated memory, then why does it still give a segmentation fault when I try to call both the functions together in main function?
有人建议可以什么是在code回事?
Can someone suggest what is going wrong in the code?
推荐答案
您不要初始化尺寸
,因此它的价值将是的不确定的你将有未定义行为的当您使用它来创建数组。
You don't initialize size
, therefore its value will be indeterminate and you will have undefined behavior when you use it to create your arrays.
未定义的行为使你的整个程序病态的。
Undefined behavior makes your whole program ill-formed.
这篇关于分割故障 - 两个功能不同时运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!