Do the return value (type ssize_t) of the C getline function and the second argument *n (type size_t) contain the same information, after invocation? Empirically, it seems that *n equals
(size_t)pow(2, ceil(log2(<return value> + 1))). Is this relation true in general? Can someone explain its (in)validity, conceptually?
 
    
    - 813
- 8
- 18
- 
                    3Have you consulted `man getline`? – Fiddling Bits Aug 18 '22 at 15:10
- 
                    Yes, I have. Still having trouble. – fmg Aug 18 '22 at 15:13
- 
                    You state in your question that, in your cases, `*n` has generally held a value different from the return value, so no they don't contain the same information. – Thomas Jager Aug 18 '22 at 15:15
- 
                    As for the relation, it seems that your specific implementation chooses to make power-of-2 size allocations when resizing, according to what's needed. This is not a requirement, but may be a common choice. The buffer just needs to be large enough. – Thomas Jager Aug 18 '22 at 15:17
- 
                    @ThomasJager By "contain the same information" I mean each can be computed from the other. Paraphrasing your second comment: the numerical relationship is implementation dependent, but uniquely characterized by the buffer length being the minimal one that is 1) big enough and 2) a power of two. Sounds like an answer to me! – fmg Aug 18 '22 at 15:20
- 
                    1@fmg "the buffer length being the minimal one" --> no it could be 0 for select reasons and 0 is not a power-of-2. – chux - Reinstate Monica Aug 18 '22 at 15:29
- 
                    @fmg Why is the value of `*n` as potentially a power-of-2 useful to know for your code? It not relying on _return_value_< `*n` enough? – chux - Reinstate Monica Aug 18 '22 at 15:33
- 
                    @fmg How would you compute the return value from `*n`? – Thomas Jager Aug 18 '22 at 15:42
- 
                    @ThomasJager You're right. You just get an upper bound. So the answer to my "do they contain the same information" question is "no". More subtle than I initially suspected! – fmg Aug 18 '22 at 15:52
- 
                    @chux-ReinstateMonica I don't need this in my code; just trying to understand things. Your point regarding the degenerate case is well taken. – fmg Aug 18 '22 at 15:53
1 Answers
ssize_t getline(char **lineptr, size_t *n, FILE *stream);
Do the return value (type ssize_t) of the C getline function and the second argument *n (type size_t) contain the same information, after invocation?
No.  The return value is the count of characters read, not including the appended null character.  *n is the size of the current allocation of *lineptr.  The return value is signed and is -1 when an (allocation) error/end-of-file occurs.  *n is an unsigned type.
It is expected that the return value is always less than *n.
it seems that *n equals (size_t)pow(2, ceil(log2( + 1))). Is this relation true in general?
No, *n may or may not be a power of 2.
getline() is not part of the C standard library and implementations differ on allocation details.
*n equals (size_t)pow(2, ceil(log2(<return value> + 1))) is invalid when:
- return value == -1 
- getline()does not re-allocate and the passed in size was not a power-of-2.
- getline()reallocates and is not using a power-of-2 scheme.
- Pedantic: Very large return value round down in the conversion to - doublein the- log2(_return value_ + 1)step.
- ... 
 
    
    - 143,097
- 13
- 135
- 256