Author: Stan Eisenstat
Subject: Re: [Cs223] example.c getline and free
Date: Friday, 20 Mar 2020, 19:49:47
> Message Posted By: Unknown
>
> could you explain what is going on in example.c with regard to getline()
> and free(line)?
getline() reads a line and stores it in a block of
malloc()-ed storage. When the address of the block is
added to or pushed on a Deque, that storage cannot be
freed until it has been printed. But when getline()
reports the end of the input, the block must be freed
or it will be lost.
=====
> line is set to NULL initially, before getline() is called for each line in
> stdin. each time getline() is called, is new memory allocated? what is
> line during each call? why is only one free() required?
Setting line = NULL forces getline() to malloc() a new
block of storage when reading the next line. As above,
that block can only be freed on an EOF.
=====
> then, at the next chunk, why can line be used again so many times?
line is a char* and can be assigned a value as often as
desired.
--Stan-
PREV
INDEX
NEXT