I have recently encountered very weird behaviour with my c program. I gutted out most of the code just to isolate where the issue is happening for you guys.
The purpose of the program in its current state is to read in jobs from a txt file and print the contents to the screen. Here it is:
#include <stdio.h>
int main(){
char* user;
char process;
int arrival;
int duration;
scanf("%*[^\n]"); //skipping header in the file.
while(!feof(stdin))
{
scanf("%s\t%c\t%d\t%d\n", user, &process, &arrival, &duration);
printf("%s\t%c\t%d\t%d\n", user, process, arrival, duration);
}
int x[5]; //<----- Causing the weird behaviour
}
The file I pipe into it is:
User Process Arrival Duration
Jim A 2 5
Mary B 2 3
Sue D 5 5
Mary C 6 2
The issue that I'm running into is whenever I declare the int x array, whether it is at the bottom or top of my code, the while loop enters an infinite loop or the program seg faults.
It will go into an infinite loop or seg fault depending on the size I declare the array at. For example, if I declare the array to be size 5, it enters an infinite loop. However, if I declare the array to be size 2, it will seg fault.
I am compiling my program by running:
gcc -o myprog myprog.c
and I am piping the file by running:
cat jobs.txt | ./myprog
It is also worth noting that the program runs fine without the int x array declaration.
I am completely stumped as to what might be the issue, any thought?