[英]What happens when I pass 0 as the second parameter of getline?
cplusplus.com states that the second parameter of the getline function is the cplusplus.com声明getline函数的第二个参数是
Maximum number of characters to write to s
要写入s的最大字符数
However, I've seen code like this: 但是,我见过这样的代码:
size_t linecap = 0;
ssize_t linelen;
linelen = getline(&line, &linecap, fp);
Won't this be reading 0 bytes from source? 这不会从源读取0个字节吗? Or is there something else going on?
或者还有其他事情发生了吗?
No, it's not correct. 不,这不对。 From the man page , ( emphasis mine )
从手册页 ,( 强调我的 )
If
*lineptr
isNULL
, thengetline()
will allocate a buffer for storing the line, which should be freed by the user program.如果
*lineptr
为NULL
,则getline()
将分配用于存储该行的缓冲区,该缓冲区应由用户程序释放。 (In this case, the value in*n
is ignored.)(在这种情况下,忽略
*n
的值。)Alternatively, before calling
getline()
,*lineptr
can contain a pointer to a malloc(3)-allocated buffer*n
bytes in size.或者,在调用
getline()
之前,*lineptr
可以包含指向malloc(3)分配缓冲区*n
字节大小的指针。 If the buffer is not large enough to hold the line,getline()
resizes it withrealloc(3)
, updating*lineptr
and*n
as necessary.如果缓冲区不足以容纳该行,则
getline()
使用realloc(3)
调整其大小,根据需要更新*lineptr
和*n
。In either case, on a successful call,
*lineptr
and*n
will be updated to reflect the buffer address and allocated size respectively.在任何一种情况下,在成功调用时,
*lineptr
和*n
将被更新以分别反映缓冲区地址和分配的大小。
So, the initial value stored in the memory pointed to by the second argument has no effect on the actual scanning. 因此,存储在第二个参数指向的内存中的初始值对实际扫描没有影响。 After the value is scanned and filled into the buffer,
扫描值并填入缓冲区后,
*n
will tell you the size of the buffer which was allocated to store the input (which is usually bigger than the size of the scanned input). *n
的值将告诉您分配用于存储输入的缓冲区的大小(通常大于扫描输入的大小)。 The idea of getline
is that there is as few as possible reallocations, as calls to malloc
tend to be expensive. getline
的想法是尽可能少地重新分配,因为对malloc
调用往往很昂贵。 Hence if you use getline
repeatedly to read the lines in a file, reusing the same buffer and length, the buffer will be eventually grown to the size of the longest line in the file, and no reallocations will be needed for the lines succeeding the longest line. 因此,如果重复使用
getline
来读取文件中的行,重用相同的缓冲区和长度,缓冲区最终将增长到文件中最长行的大小,并且对于最长行之后的行不需要重新分配线。
But for that to work certain contracts must be followed - namely if *lineptr
is non-NULL then it 但为了工作,必须遵循某些合同 - 即如果
*lineptr
是非NULL然后它
malloc
malloc
返回的指针 *n
bytes *n
个字节的分配大小 Corollary: passing 0
in *n
is fine under these 2 circumstances: 推论:在这两种情况下,在
*n
传递0
是正常的:
*lineptr
is NULL
*lineptr
为NULL
*lineptr
is any live pointer returned by malloc
(as any pointer returned by malloc
will have 0 bytes space). *lineptr
是由返回的任何活的指针malloc
(如通过返回的任何指针malloc
将具有0字节空间)。 in both cases *n
will be updated to the length of the line, and the return value of realloc(*lineptr, new_line_length_with_terminator)
(if successful) will have been assigned to *lineptr
. 在这两种情况下,
*n
将更新为行的长度,并且realloc(*lineptr, new_line_length_with_terminator)
的返回值(如果成功)将被分配给*lineptr
。
Of course 当然
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.