On Thu, Jan 24 2019 13:58:04 +0200, Lauri Tirkkonen wrote: > And even then I think the cost is negligible: getline grows > the buffer in powers of 2, so only lines that happen to be twice as long > as any previously encountered line pay the price.
Actually, I went to skim the fgetln() implementation and it might very well be that fgetln() does *more* reallocations than getline. The storage that fgetln() uses is hidden from the user, but obviously it has to store its buffer somewhere. It's a member of struct FILE: 125 /* separate buffer for fgetln() when line crosses buffer boundary */ 126 struct __sbuf _lb; /* buffer for fgetln() */ and it is expanded by __slbexpand() in stdio/fgetln.c, which just does recallocarray(). To me it looks like the buffer is either incremented by OPTIMISTIC (#define'd to be 80 bytes), or again by exactly as much as is required by the current line if that wasn't enough. I haven't done any actual measurements though, so it's possible my reading is wrong. -- Lauri Tirkkonen | lotheac @ IRCnet