开发者

Segmentation fault on function call

开发者 https://www.devze.com 2023-03-19 08:33 出处:网络
I am trying to create a simulation of a stochastic queue system. The thing is that when running the file, I get a segmentation fault as soon as it tries to call update(). I figured it\'s a stack probl

I am trying to create a simulation of a stochastic queue system. The thing is that when running the file, I get a segmentation fault as soon as it tries to call update(). I figured it's a stack problem because the code in the function runs fine but since I am not very experienced with C I'd like your help too. Here is the code:

#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);
       开发者_StackOverflow中文版                     } 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;
}

Please don't mind the rest of the errors. I am currently in the debugging stage, so any help would be greatly appreciated, thanks.


Your problem is in line:

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

Here i is uninitialized and p_help_a[i] leads to access violation.


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

what if they are null?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号