This question is definitely related to this and it's answer is what I based my function off of.
char *get_next_line(FILE *fp) {
char ch = 0;
int CUR_MAX = 4095;
char *buffer = (char*) malloc(sizeof(char) * CUR_MAX); // allocate buffer.
char *temp = (char*) malloc(sizeof(char) * CUR_MAX); // allocate buffer.
int count = 0;
int length = 0;
while ((ch != '\n')) {
if (ch == '\377') { return NULL; }
if(count ==CUR_MAX) {
CUR_MAX *= 2;
count = 0;
if ((temp = realloc(buffer, CUR_MAX)) != NULL) {
buffer = temp;
free(temp);
}
}
ch = getc(fp);
buffer[length] = ch;
length++;
count++;
}
For some reason, when reading in very large strings I'm faced with:
glibc detected - realloc() invalid next size.
Is there something I'm missing here?
Thanks!