C 总线错误,don';我不知道该怎么办
我正在为我在学校的一项作业写一个程序来模拟耦合谐振子的振动(一个块通过弹簧连接到墙上,另一个块通过弹簧连接到第一个块)。通过这个扩展,我选择了n个弹簧连接的n个模块!在合理的数量下,我开始尝试3 当我试图编译它的时候,它编译得很好,我可以运行只需要输入的第一部分的程序。之后它就崩溃了,出现了总线错误。这是我的代码,很抱歉它看起来像一堵墙!!它首先从命令中获取大量值,如块的位置C 总线错误,don';我不知道该怎么办,c,bus-error,C,Bus Error,我正在为我在学校的一项作业写一个程序来模拟耦合谐振子的振动(一个块通过弹簧连接到墙上,另一个块通过弹簧连接到第一个块)。通过这个扩展,我选择了n个弹簧连接的n个模块!在合理的数量下,我开始尝试3 当我试图编译它的时候,它编译得很好,我可以运行只需要输入的第一部分的程序。之后它就崩溃了,出现了总线错误。这是我的代码,很抱歉它看起来像一堵墙!!它首先从命令中获取大量值,如块的位置 #include <stdlib.h> #include <stdio.h> #in
#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);
}
#包括
#包括
#包括
/*为块的宽度和弹簧的静止长度设置变量*/
#定义w1.0
#定义R1.0
/*定义查找龙格库塔常数的外部函数*/
双rkutta(双*xpos,双*omeg,双*j,int delta,int n,int n);
int main(int argc,char*argv[])
{
文件*输出;
int-tmp,N;
双*x,*v,*m,*k,*omega,mtmp,ktmp,t,dt,*xstep,*vstep;
/*从命令行中查找质量数的值*/
N=argc-1;
dt=0.001;
/*为所需的每个变量分配阵列内存*/
x=malloc(N*sizeof(double));
v=malloc(N*sizeof(双));
m=malloc(N*sizeof(double));
k=malloc(N*sizeof(double));
ω=malloc((2*N-1)*sizeof(双精度));
/*从命令行读取每个体量的x位置的值*/
如果(x!=NULL&&v!=NULL&&m!=NULL&&k!=NULL&&omega!=NULL)
{
对于在调试器中运行的(tmp=0;tmp),我在这里失败:
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个文件创建内存).总线错误通常意味着您访问内存的方式不正确,例如取消引用未初始化的指针。单步执行调试器可以帮助您跟踪错误点。嗯,我不太确定您的意思。什么调试器?我该如何单步执行代码?您使用的是哪种编译器/操作系统?gcc到m在y学校,我通过PuTTY连接到他们的服务器带有gcc的调试工具叫做gdb
。你可以像这样运行:gdb a.out
(或者你的程序叫什么名字)。请确保在编译时使用-g
,以便在程序文件中包含调试信息。好的,谢谢你,我看到了我所做的一切。也谢谢你向我展示调试工具!我目前不知道如何使用它,但我可以学习!啊,我更改了它,现在我遇到了一个分段错误:之后还有更多问题更改这些,仍然存在SEGFULTS。Yer注意到xstep和vstep的问题是相同的,更改了它。仍然是SEGFULTS好的,我已经解决了问题,我发送了一个整数作为指向外部函数的指针。感谢向我展示gdb,非常有用!