gpt4 book ai didi

c - 总线错误,不知道该怎么办

转载 作者:行者123 更新时间:2023-11-30 15:39:50 25 4
gpt4 key购买 nike

我正在为学校的一项作业编写一个程序,以模拟耦合谐振子的振荡(一个 block 通过 Spring 连接到墙壁,另一个 block 通过 Spring 连接到第一个 block )。有了这个,我选择的扩展是对由 n 个 Spring 连接的 n 个 block 进行建模!在合理的数量范围内,我首先尝试了 3 个。

当我尝试编译它时,它编译得很好,我可以运行程序的第一部分,只需输入。之后它就崩溃了,给出了总线错误。这是我的代码,抱歉它看起来像一堵墙!它首先从命令中获取一些值,例如 block 的位置。

#include    <stdlib.h>
#include <stdio.h>
#include <math.h>

/* Set variables for the width of the blocks and rest length of the springs*/
#define w 1.0
#define R 1.0

/*Define our external function for finding the runge kutta constants*/
double rkutta(double *xpos, double *omeg, double *j, int delta, int n, int N);

int main(int argc, char *argv[])
{
FILE* output;
int tmp, N;
double *x, *v, *m, *k, *omega, mtmp, ktmp, t, dt, *xstep, *vstep;

/*Find value of number of masses from the command line*/
N = argc - 1;
dt = 0.001;

/*Allocate array memory for each variable required*/
x = malloc(N*sizeof(double));
v = malloc(N*sizeof(double));
m = malloc(N*sizeof(double));
k = malloc(N*sizeof(double));
omega = malloc((2*N - 1)*sizeof(double));

/*Read values for the x position of each mass from command line*/
if(x != NULL && v != NULL && m != NULL && k != NULL && omega !=NULL)
{
for(tmp=0; tmp<N; tmp++)
{
sscanf(argv[tmp+1], "%lf", &x[tmp]);
}
}
else
{
printf("***************************\n");
printf("**Error allocating arrays**\n");
printf("***************************\n");
}

/*Check there are an appropriate amount of masses,
if so take values for other quantities from user*/
if(N <= 1)
{
printf("************************************\n");
printf("**There must be at least 2 masses!**\n");
printf("************************************\n");
}
else if(N == 2)
{
for(tmp=0; tmp<N; tmp++)
{
printf("Input a value for the velocity of Block %d\n", tmp+1);
scanf("%lf", &v[tmp]);
}

for(tmp=0; tmp<N; tmp++)
{
printf("Input a value for the mass of Block %d\n", tmp+1);
scanf("%lf", &m[tmp]);
}

for(tmp=0; tmp<N; tmp++)
{
printf("Input a value for the spring constant of Spring %d\n", tmp+1);
scanf("%lf", &k[tmp]);
}
}
else
{
for(tmp=0; tmp<N; tmp++)
{
printf("Input a value for the velocity of Mass %d\n", tmp+1);
scanf("%lf", &v[tmp]);
}

printf("Input a value for the mass of each Block\n");
scanf("%lf", &mtmp);
for(tmp=0; tmp<N; tmp++)
{
m[tmp] = mtmp;
}

printf("Input a value for the spring constant of each Spring\n");
scanf("%lf", &ktmp);
for(tmp=0; tmp<N; tmp++)
{
k[tmp] = ktmp;
}
}

/*Compute values of each omega*/
for(tmp=0; tmp<(2*N-1); tmp++)
{
if(tmp % 2)
{
omega[tmp] = k[(tmp+1)/2] / m[(tmp-1)/2];
}
else
{
omega[tmp] = k[tmp/2] / m[tmp/2];
}
}

/*Define arrays for runge kutta constants*/
double *a, *b, *c, *d;

/*Calculate the values of the runge kutta constants*/
for(tmp=0; tmp<(2*N); tmp++)
{
if(tmp < N)
{
a[tmp] = v[tmp];
}
else
{
a[tmp] = rkutta(x, omega, 0, 0, (tmp-N), N);
}
}
for(tmp=0; tmp<(2*N); tmp++)
{
if(tmp < N)
{
b[tmp] = v[tmp] + 0.5*dt*a[tmp+2];
}
else
{
b[tmp] = rkutta(x, omega, a, (0.5*dt), (tmp-N), N);
}
}
for(tmp=0; tmp<(2*N); tmp++)
{
if(tmp < N)
{
c[tmp] = v[tmp] + 0.5*dt*b[tmp+2];
}
else
{
c[tmp] = rkutta(x, omega, b, (0.5*dt), (tmp-N), N);
}
}
for(tmp=0; tmp<(2*N); tmp++)
{
if(tmp < N)
{
d[tmp] = v[tmp] + dt*c[tmp+2];
}
else
{
d[tmp] = rkutta(x, omega, c, dt, (tmp-N), N);
}
}

/*Open file to output data*/
output = fopen("1209937_proj1.out", "w");

for(t=0; t<=0.1; t=t+dt)
{

if(output != (FILE*)NULL)
{
fprintf(output, "%lf ", t);
for(tmp=0; tmp<N; tmp++)
{
fprintf(output, "%lf ", x[tmp]);
}
for(tmp=0; tmp<N; tmp++)
{
if(tmp<N-1)
{
fprintf(output, "%lf ", v[tmp]);
}
else
{
fprintf(output, "%lf\n", v[tmp]);
}
}
}
else
{
printf("*********************************\n");
printf("**Error outputting data to file**\n");
printf("*********************************\n");
return(EXIT_FAILURE);
}

/*Use runge kutta to find the next value of v and x*/
for(tmp=0; tmp<N; tmp++)
{
xstep[tmp] = x[tmp] + (dt/6)*(a[tmp]+2*b[tmp]+2*c[tmp]+d[tmp]);

vstep[tmp] = v[tmp] + (dt/6)*(a[tmp+2]+2*b[tmp+2]);
vstep[tmp] = vstep[tmp] + (dt/6)*(2*c[tmp+2]+d[tmp+2]);

x[tmp] = xstep[tmp];
v[tmp] = vstep[tmp];
}
}

free(x);
free(v);
free(m);
free(k);
free(omega);
fclose(output);
return(EXIT_SUCCESS);
}

/*Given various quantities find runge kutta values*/
double rkutta(double *xpos, double *omeg, double *j, int delta, int n, int N)
{
int temp;
double result;

result = 0;

for(temp=0; temp<N; temp++)
{
xpos[temp] = xpos[temp] + delta*j[temp];
}

if(n=0)
{
result = -omeg[n]*(xpos[n]-R) + omeg[n+1]*(xpos[n+1]-xpos[n]-w-R);
}
else if(n < N-1)
{
result = -omeg[2*n]*(xpos[n]-xpos[n-1]-w-R);
result = result + omeg[(2*n+1)]*(xpos[n+1]-xpos[n]-w-R);
}
else
{
result = -omeg[(2*n-1)]*(xpos[n]-xpos[n-1]-w-R);
}

return(result);
}

我并没有真正做过太多编程,所以如果这是显而易见的事情,我很抱歉。我只是想修复它。程序将一直运行,直到用户输入所有数据为止。

最佳答案

在调试器中运行对我来说失败了:

Program received signal SIGSEGV, Segmentation fault.
0x0000000000400cfb in main (argc=3, argv=0x7fffffffe508) at t.c:116
116 a[tmp] = v[tmp];

查看a,我们发现它未初始化。

(gdb) p a
$1 = (double *) 0xc2

查看您的程序,我们发现您定义了:

/*Define arrays for runge kutta constants*/
double *a, *b, *c, *d;

但是您还没有定义数组,您定义了一个空指针并且没有将其设置为任何内容。

将其更改为

double a[100], b[100], c[100], d[100] ;

然后编译器将为这些创建内存(至少为 N 到 50)。

关于c - 总线错误,不知道该怎么办,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21290845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com