0

I keep on getting this Bus Error 10 on my code and I have no idea why.

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

#define SIZE 100

int main() {

    char input[SIZE];

    int dummy[SIZE];

    int array[SIZE][SIZE][SIZE];

    int set=-1;
    int sequence=0;


    while (1==1) {
       if (fgets(input, SIZE-1, stdin) == NULL){
         printf("Input Error.");
         break;
       }else {

         char* s;

         for (s = input; (*s != '\n') && isspace(*s); s++){
            ; 
         }

         if (*s == 'f'){ //start of finish

            break; 

         } else if (*s == 'S'){ //start of SET

            set++;

         } else{

            sscanf(input, "%d: %d, %d, %d, %d, %d", &dummy[0], 
            &dummy[1], &dummy[2], &dummy[3], &dummy[4], 
            &dummy[5]);

            array[set][sequence][0]=dummy[0];

            array[set][sequence][1]= 
            dummy[1]+dummy[2]+dummy[3]+dummy[4]+dummy[5];

            sequence++;

         }

       }
     }

    for (int i=0; i<sizeof(array); i++ ){
        printf("SET %d\n", i+1);
        for (int j=0; j<sequence;j++){
            printf("%d", array[i][j][0]);
            for (int k=0; k<=5; k++){
                printf("%d\n", array[i][j][1]);
            }
        }
    }

    return 0;
}

Basically if I have an input of a certain number of sets and a sequence of numbers. Like so:

SET 1

1: 5,5,6,5
2: 3,4,5,5
3: 3,4,5,6

SET 2

1: 5,7,8,7
2: 5,5,5,6


finish

I want to output the sequence number and sum of all the number after the colon for each sequence in a given set. So it would look something like this.

SET 1
1 21
2 17
3 18

SET 2
1 27
2 21

But, when I try to execute my code, I get Bus Error: 10. I tried resizing the array but now I can't iterate over my array since it keep returning a bunch of random number. Like:

-34618613832766
32766
32766
32766
32766
32766
1178023731825050672
825050672
825050672

Where are these numbers from? thanks.

William
  • 23
  • 6

1 Answers1

1

Your arrays are most likely too big for the stack. Consult your compilers manual on how to increase the stack size or allocate them dynamically on the free store (aka "heap") using malloc().

Swordfish
  • 12,971
  • 3
  • 21
  • 43