Измените ваш код на:
char *str = NULL; // realloc can be called with NULL
size_t len = 1; /* I also count the terminating character */
char temp;
while ((temp = getc(file)) != EOF)
{
str = (char *)realloc(str, ++len * sizeof(char));
str[len-2] = temp;
str[len-1] = '\0';
}
Ваша проблема в том, что вы звонили realloc
с указателем на память, которая не была выделена либо malloc
, либо realloc
, что недопустимо.
Из справочной страницы realloc
:
realloc() changes the size of the memory block pointed to by ptr to size bytes.
The contents will be unchanged to the minimum of the old and new
sizes; newly allocated memory will be uninitialized. If ptr is NULL,
then the call is equivalent to malloc(size), for all values of size;
if size is equal to zero, and ptr is not NULL, then the call is
equivalent to free(ptr). Unless ptr is NULL, it must have been
returned by an earlier call to malloc(), calloc() or realloc(). If
the area pointed to was moved, a free(ptr) is done.
Кстати, вы не должны увеличивать буфер по одному символу за раз, а должны иметь два счетчика, один для емкости буфераи один для количества используемых символов и увеличивает буфер только тогда, когда он заполнен.В противном случае ваш алгоритм будет иметь очень низкую производительность.