简体   繁体   English

当我将0作为getline的第二个参数传递时会发生什么?

[英]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 is NULL , then getline() will allocate a buffer for storing the line, which should be freed by the user program. 如果*lineptrNULL ,则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 with realloc(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, 扫描值并填入缓冲区后,

  • the function return value will tell you the size of the scanned input in bytes. 函数返回值将以字节为单位告诉您扫描输入的大小。
  • the value of *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然后它

  • must be a pointer returned by malloc 必须是malloc返回的指针
  • must have allocation size of at least *n bytes 必须具有至少*n个字节的分配大小

Corollary: passing 0 in *n is fine under these 2 circumstances: 推论:在这两种情况下,在*n传递0是正常的:

  • if *lineptr is NULL if *lineptrNULL
  • *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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM