Reading tokenized numbers from a file int *getUserInput(FILE *fpin, int *elementCount) {
  int *userInput;
  // Load file into buffer
  int length = fileLength(fpin);
  char *buffer = (char *) malloc(length * sizeof(char));
  int written = fread(buffer, sizeof(char), length, fpin);
  // Number of int's allocated in userInput
  int allocated = 10;
  *elementCount = 0;
  // Allocate space for the numbers
  userInput = (int *) malloc(allocated * sizeof(int));
  // "tokenize" the buffer with " " and "\n" as delimiters
  char *tokenized = strtok(buffer, " \n");
  while (tokenized != NULL) {
   if (allocated == *elementCount) {
      allocated *= 2;
      // Reallocate space based on need
      realloc(userInput, allocated * sizeof(int));
    }
    // Store the number in the array
    userInput[(*elementCount)++] = atoi(tokenized);
    tokenized = strtok(NULL, " \n");
  }
  realloc(userInput, *elementCount * sizeof(int));
  free(buffer);
  return userInput;
}


It's a function I wrote for reading in numbers from a file, delimited by a space. Curious to hear any thoughts/possible improvements.
Check that malloc didn't return NULL; in general, use defensive programming; That means always assume things went wrong, and check all return values. It helps with debugging too.

You should be aware that strtok and atoi are not safe functions for arbitrary user input. That means you are vulnerable to stack/buffer overflows. Read the man pages of the two functions carefully. Consider using \0-safe functions instead, or at least put a \0 at the end of the input strings.