c - 函数调用的段错误

标签 c function segmentation-fault

我正在尝试创建随机队列系统的模拟。问题是,在运行文件时,一旦它尝试调用 update(),我就会遇到段错误。我认为这是一个堆栈问题,因为函数中的代码运行良好,但由于我对 C 不是很有经验,所以我也需要你的帮助。这是代码:

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

#define MAXCLIENTS 11
#define COUNTER_LIMIT 80000000
#define MEAN_DIFFERENCE 0.0000001

char servers_enabled;           
int state = 0;              
float p[MAXCLIENTS];            
float p_help_a[MAXCLIENTS];
float p_help_b[MAXCLIENTS];     
int arrivals = 0;               
int arrival_a[MAXCLIENTS];      
int arrival_b[MAXCLIENTS];
long int count = 0;             
float lambda[3]={0.5,1.0,1.5};  
float mi_a = 2,mi_b = 0.5;      
float gamma_a = 0,gamma_b = 0,gamma_ratio=0; 
float p_b_ab,p_a,p_b,p_a_ab;            
double E = 0,lastE = 0.1;                   

void arrive(char queue_enabled) {       
    ++arrivals;
    switch (queue_enabled) {
    case 'a' :
        ++arrival_a[state];
        break;
    case 'b' :
        ++arrival_b[state];
        break;
    }
    ++count;
    if (state<MAXCLIENTS)
        ++state;
}

void depart() {     
    ++count;
    --state;
}

float randomize() {
    float result;
    srand(time(NULL));
    result = (float) ((float)rand()/INT_MAX);
    return result;
}

void update(FILE *input,FILE *output, FILE *helper) {   
    int i;
    if (count%50000 == 0) {
        gamma_a=0;
        gamma_b=0;
        for (i=0;i<MAXCLIENTS;i++) {
            p_help_a[i] = (float) (arrival_a[i]/arrivals);
            p_help_b[i] = (float) (arrival_b[i]/arrivals);
            p[i] = p_help_a[i]+p_help_b[i];
            gamma_a += p[i]*mi_a;
            gamma_b += p[i]*(arrival_b[i]/arrivals);
            if (E != 0)
                lastE = E;
            E = 0;
            E += i*p[i];
        }
        gamma_a-=p_help_b[1]*mi_a-p_help_a[0];
        if (gamma_b != 0.0)
            gamma_ratio = gamma_a/gamma_b;
        fprintf(output,"throughput_a = %f   | throughput_b = %f     | throughput ratio = %8.6f",gamma_a,gamma_b,gamma_ratio);
        fprintf(output,"    | E = %f    | Pli8os Gegonotwn = %ld\n",E,count);
    }
    //count++;
    fprintf(input,"p_a[%2d] = %.10f p_b[%2d] = %.10f \n",i,p_help_a[i],i,p_help_b[i]);
    fprintf(input,"p = %f \n",p[i]);
    fprintf(helper,"gamma_a= %f    gamma_b= %f    gamma_ratio = %8.6f ",gamma_a,gamma_b,gamma_ratio);
    fprintf(helper,"    E= %f      count= %ld \n",E,(count-1));
}

int main() {

    int i,j,k;      
    FILE *input,*output,*helper;

    input=fopen("first.txt","w");
    output=fopen("second.txt","w");
    helper=fopen("third.txt","w");


    for (i=0;i<3;i++) { 
        fprintf(input,"Ruthmos Afiksewn (lambda) =   %1.1f\n\n",lambda[i]);
        fprintf(output,"Ruthmos Afiksewn (lambda) =   %1.1f\n\n",lambda[i]);
        fprintf(helper,"Ruthmos Afiksewn (lambda) =   %1.1f\n\n",lambda[i]);
        p_a=lambda[i]/(lambda[i]+mi_a);                      p_a_ab=lambda[i]/(lambda[i]+mi_a+mi_b); 
        p_b=lambda[i]/(lambda[i]+mi_b);     
        p_b_ab=(lambda[i]+mi_a)/(lambda[i]+mi_a+mi_b); // ????
        for (j=1;j<=10;j++) {       
            fprintf(input,"--------------------------\n\n");
            fprintf(input,"Katofli (threshold) =   %d\n\n",j);
            fprintf(input,"--------------------------\n\n");
            fprintf(output,"--------------------------\n\n");
            fprintf(output,"Katofli (threshold) =   %d\n\n",j);
            fprintf(output,"--------------------------\n\n");
            fprintf(helper,"--------------------------\n\n");
            fprintf(helper,"Katofli (threshold) =   %d\n\n",j);
            fprintf(helper,"--------------------------\n\n");

            for (k=0;k<(MAXCLIENTS-1);k++) {
                /******************/
            }
            servers_enabled = 'a';
            while(count<=COUNTER_LIMIT && fabs(E-lastE)>=MEAN_DIFFERENCE) {
                if (state == 0) {
                    arrive('a');
                    update(input,output,helper);
                } else
                switch (servers_enabled) {
                    case 'a' :
                        if ((state <= j) && randomize() <= p_a) {
                            arrive('a');
                            update(input,output,helper);
                        } else {
                            depart();
                            update(input,output,helper);
                        }
                        if (state > j)
                            servers_enabled = 'b';
                        break;
                    case 'b' :
                        if (state == 1) 
                            if (randomize() < p_b) {
                                arrive('b');
                                update(input,output,helper);
                            } else {
                                depart();
                                update(input,output,helper);
                            } else if (randomize() < p_a) {
                                arrive('b');
                                update(input,output,helper);
                            } else if (randomize() < p_b_ab) {
                                depart();
                                update(input,output,helper);
                            } else {
                                depart();
                                update(input,output,helper);
                                if (state <= j)
                                    servers_enabled = 'a';
                            }
                        break;
                }
            }
        }
    }
    fclose(input);
    fclose(output);
    fclose(helper);
    return 0;
}

请不要介意其余的错误。我目前正处于调试阶段,因此非常感谢任何帮助,谢谢。

最佳答案

你的问题是:

fprintf(input,"p_a[%2d] = %.10f p_b[%2d] = %.10f \n",i,p_help_a[i],i,p_help_b[i]);

此处i未初始化,p_help_a[i]导致访问冲突。

关于c - 函数调用的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6640421/

相关文章:

python - 在另一个函数调用中调用一个函数

python - 暴露 __main__

c - 在 C 中使用指针和 malloc 的两个暗淡数组中的运行时错误

c - 分散后出现段错误

c - 将字符数组指定为函数参数的正确语法是什么?

c - Makefile - 在单独的文件中包含路径

c - 为什么我的程序在使用 scanf 后暂停?

c - 指针已释放但未分配

Javascript 帮助 : Functions/Objects

segmentation-fault - 尝试使用 Composer 安装 Drush 时出现段错误