#include <stdlib.h> #include <stdio.h> #include <string.h> int temp; int main() { FILE * fp; fp = fopen("input2.txt", "r"); //Open the input int counter = 0; int realloc_counter = 10; int *line_array; //Initialize the array line_array = malloc(10 * sizeof(int)); //Allocate memory for initial ten numbers, of size int for each while (fscanf(fp, "%d", &temp) > 0) { line_array[counter] = temp; counter ++; if (counter % 10 == 0) { realloc_counter = realloc_counter * 2; line_array = realloc(line_array, realloc_counter); } } fclose(fp); //Close the input file free(line_array); //Free the memory
The above code is what I have. It keeps giving me an error and I can't seem to figure it out. Using valgrind it says there is an invalid write of size 4. Any suggestions or insight?