You are trying to access to a bad index array[length]=num;
, if the size of array is length
, you can't access to length
-th element.
You have to move the write after the reallocation :
/*Check for valid inputs and put into array*/
while((scanf("%d", &num)) != EOF){
length++;
array = realloc(array, length*sizeof(int));
if(array == NULL)
pb_memory();
array[length-1]=num;
}