Every time I run this code, my program just ups and crashes. I am just learning C and all this and not sure if it is my computer (least likely) or my code (more likely). Could really use the help in figuring this out. Thank you.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>
struct node{
void * curr;
struct node * NEXT;
};
char * FArray(char * array){//fills the array with Random char
for(int r = 0; r < 999999; r++){
char n = rand() % 26 + 65; //random generator A - Z
array[r] = n;
}
return array;
}//FArray
char * dequeue (struct node ** front, struct node ** tail){
//remove the front of the line
struct node * ShortTemNode = (struct node*)malloc(1000000*sizeof(8)); //node with 1000000 structure
ShortTemNode -> NEXT = NULL; //next become null
if((*front) != NULL){
//Point to the string being dequeued
char * TheString = (*front) -> curr;
ShortTemNode = (*front) -> NEXT;
//Current line leader is removed from front and next is assigned to the front
free(*front);
*front = ShortTemNode;
return TheString;
}//if ends here - Not empty? Point to dequeued string
else{
return NULL;//else just return NULL
}//else ends here
};//dequeue ends here
int enqueue (char * string, struct node ** front, struct node ** tail){
//place at the back of the line
struct node * ShortTempNode = (struct node*)malloc(1000000*sizeof(8)); //node created
ShortTempNode -> NEXT = NULL;
ShortTempNode -> curr = string;
if((*front) == NULL){
*front = ShortTempNode;
*tail = ShortTempNode;
}//if end here
//adds on to the end of the list
else{
(*tail) -> NEXT = ShortTempNode;
*tail = ShortTempNode;
}
return 0;
};//enqueue end here
int main (int argc, const char * argv[]) {
int TimCount = 0; //count number of dequeues happening
int r;
int y;
int a;
struct node * front;//front created
struct node * tail;//tail created
for(r = 0; r < 10; r++){
char array [1000000];//array of 1,000,000
char * string = FArray(array); //points to string
enqueue(string, &front, &tail);
}//for ends here
//enqueue and dequeue 99990
for(y = 0; y < 99990; y++){
char array [1000000]; //array that stores
char * string = FArray(array);
enqueue(string, &front, &tail);
dequeue(&front, & tail);
TimCount++;
}//for ends here
for(a = 0; a < 10; a++){
dequeue(&front, &tail);
//count the number of times dequeued
TimCount++;
//tail is dequeued 10 times
}//for ends here
printf("%d", TimCount); fflush(stdout); //Print out one line of output
}//main