Home > Net >  Bus error while simply integer scanf sequence in C program
Bus error while simply integer scanf sequence in C program

Time:08-11

I have to take integer inputs and end the while loop if input is -1 in the following format in C:

111 1 1
111 1 1
111 1 1
-1

My code get bus error after 3rd scanf in the main function (end of the code). I know this is about white spaces but i have no idea to solve it. I am testing on MacBook with M1 if it is important. Btw print("break")s for just testing, i don't want to any junk print or scan.

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

struct employee{
    int ID;
    int freeAt;
    int totalTime;
    struct employee *next;
};

struct customer{
    int ID;
    int startTime;
    int processTime;
    int waitingTime;
    int helper;
    struct customer *front;
    struct customer *rear;
    struct customer *next;
};

typedef struct employee employee;
typedef struct customer customer;

void new_employer(employee *top, int id){ //Insert employees by id
    employee *ptr;
    ptr = (employee*)malloc(sizeof(employee));
    ptr->ID = id;
    ptr->freeAt = 0;
    ptr->totalTime = 0;

    if (top == NULL){
        ptr->next =  NULL;
        top = ptr;
    }
    else{
        ptr->next = top;
        top = ptr;
    }
}

void help_customer(customer *c, employee *top){ //Match customer with suitable employee
    employee *ptr;
    ptr = (employee*)malloc(sizeof(employee));
    ptr = top;

    if (top==NULL){
        c->startTime = c->startTime   1;
        c->waitingTime  ;
        help_customer(c,ptr);
    }

    if(c->startTime>=top->freeAt){
        c->helper = top->ID;
        top->freeAt = c->startTime   c->processTime;
        top->totalTime = top->totalTime   c->processTime;
    }
    else{
        help_customer(c, top->next);
    }
}

void new_customer(customer *c, employee *top, int id, int start, int process){ //New customer
    customer *ptr;
    ptr = (customer*)malloc(sizeof(customer));
    ptr->ID = id;
    ptr->startTime = start;
    ptr->processTime = process;
    ptr->waitingTime = 0;
    help_customer(ptr,top);
    
    if (c->front == NULL){
        c->front = ptr;
        c->rear = ptr;
        c->next = NULL;
    }
    else{
        c->rear->next = ptr;
        c->rear = ptr;
        c->rear->next = NULL;
    }
}

void customerStats(customer *c){ //Printing customer stats at the end of the transactions
    customer *ptr;
    ptr = (customer*)malloc(sizeof(customer));
    ptr = c->front;

    if(ptr == NULL){
        printf("\nQUEUE IS EMPTY");
    }
    else{
        printf("\n");
        while (ptr!=c->rear){
            printf("%d ", ptr->ID);
            printf("%d ", ptr->helper);
            printf("%d ", ptr->startTime);
            printf("%d ", ptr->processTime);
            printf("%d\n", ptr->waitingTime);
            ptr = ptr->next;
        }

        printf("%d ", ptr->ID);
            printf("%d ", ptr->helper);
            printf("%d ", ptr->startTime);
            printf("%d ", ptr->processTime);
            printf("%d\n", ptr->waitingTime);
    }
}

void employeeStats(employee *top){ //Printing employee stats at the end of the transactions
    employee *ptr;
    ptr = (employee*)malloc(sizeof(employee));
    ptr = top;
    
    if (top==NULL){
        printf("\nSTACK IS EMPTY");
    }
    else{
        while (ptr!=NULL){
            printf("%d ", ptr->ID);
            printf("%d\n", ptr->totalTime);
            ptr = ptr->next;
        }
    }
}



int main(){
    employee *e;
    for(int i=1;i>7;i  ){
        new_employer(e,i);
    }

    customer *c;
    int id;
    int start;
    int process;
    while(1){       //********I GET BUS ERROR HERE ON 3RD SCANF**************
        scanf("%d", &id);
        printf("break1");
        if (id==-1){
            break;
        }
        scanf("%d", &start);
        printf("break2");
        scanf("%d", &process);
        printf("break3");
        new_customer(c,e,id,start,process);
    }

    customerStats(c);
    employeeStats(e);
}

This is the terminal:

111
break11
break21
zsh: bus error  ./main

CodePudding user response:

It helps if you write minimal code to begin with, and get that working before adding more.

Here's a partial correction that shows why you are getting undefined behaviour (crashing). Read each line and understand why and how it differs from your code. Once that is understood, you can (cautiously, step-by-step) add one more element at a time and test, test, test as you build up toward your objective.

typedef struct employee {
    int ID;
    int freeAt;
    int totalTime;
    struct employee *next;
} employee_t;

employee_t *new_employeEEEE( employee_t *top, int id ) { //Insert employees by id
    employee_t *ptr = (employee_t*)calloc( 1, sizeof(*ptr) ); // use calloc()
    // Omitting check of calloc failing
    ptr->ID = id;

    ptr->next = top; // reverse sequence, but at least it works...

    return ptr;
}

int main() {
    employee_t *e = NULL;

    // for( int i=1; i > 7;i   ) !!!!

    for( int i = 1; i < 7; i   )
        e = new_employeEEEE( e, i );

    i = 1;
    for( employee *p = e; p; p = p->next )
        printf( "emp #%d has id: %d\n", i  , p->ID );

    return 0;
}

Output:

emp #1 has id: 6
emp #2 has id: 5
emp #3 has id: 4
emp #4 has id: 3
emp #5 has id: 2
emp #6 has id: 1
  • Related