【发布时间】:2016-01-02 22:09:57
【问题描述】:
#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
上面的代码就是我所拥有的。它一直给我一个错误,我似乎无法弄清楚。使用 valgrind 它说存在大小为 4 的无效写入。有什么建议或见解吗?
【问题讨论】: