Author: Stan Eisenstat
Subject: Re: [Cs223] free(line) after /dev/null getline() read
Date: Thursday, 26 Mar 2020, 08:14:35
> Message Posted By: Unknown
>
> I have a loop that reads in every file in order. Inside this loop is
> another while loop that reads in each line. The getline() function is
> within the while condition. After EOF is reached, I have free(line).
When you free line, you must reset it to NULL and
reset n to 0, so that on the next call getline() does
not think that line points at a block of malloc()-ed
storage when it is called again.
=====
> When a null file is opened after a non-empty file, the program goes
> through the getline() condition for the null file and goes directly to
> free(line). At this free call, a "double free" error is detected.
>
> Due to the nature of a null file, does getline not allocate space to be
> free'd after? This would not make sense as running the loop with just a
> null file does not produce this error.
I think that my response above explains what you are
reporting, but without being able to see and run your
code, I cannot further diagnose the problem.
--Stan-
PREV
INDEX
NEXT