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

时间:2014-01-22 18:20:01

标签: c bus-error

我正在编写一个程序用于我在学校的任务,以模拟耦合谐振子(通过弹簧连接到墙壁的块,以及通过弹簧连接到第一块的另一个块)的振荡。有了这个我的扩展,我选择的是模拟由n个弹簧连接的n个块!达到合理的数额,我开始尝试3。

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

#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);
}

我还没有做太多的编程,所以如果这是显而易见的话,我很抱歉。我只是想解决它。程序运行直到用户输入所有数据。

1 个答案:

答案 0 :(得分:1)

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

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个)。