The setup is such that you can pass the previously allocated memory block getline() , and it will allocate more ( realloc() ) if necessary. (Or you can start with the lack of allocated memory, as here.) It can report an error or EOF, but does not free up the space that was allocated, so you need to free it. If the file is an empty file and you start without data, you may not get the allocated space; on the other hand, it may allocate some space before trying to get data from a file.
But you know that if the line pointer is not null, it has been allocated and needs to be freed.
Jonathan leffler
source share