In general, the problem is as follows:
#include <sys/types.h> #include <stdio.h> #include <assert.h> #include <errno.h> char buf[5]; int main(int argc, char **argv) { ssize_t len = 0; char *buffer=buf; //buffer = realloc(buffer,10); FILE *f = fopen(argv[1],"r"); ssize_t a = getline(&buffer, &len, f); printf("%zd", a); fclose(f); return 0; } when calling the library function getline and passing the pointer to it not on the heap, the function works in normal mode and without errors. If we use our own implementation of getline (more precisely, honestly copied getdelim, for example, with getdelim , then we catch the error:
* Error in `./a.out ': realloc (): invalid pointer: 0x080497ed * Aborted
It is clear that the problem is in realloc and passing it a pointer. But can anyone know how to get around this?