内存分配和getline
memory allocation and getline
这段代码我不明白为什么*line指针没有malloc,getline是动态分配内存的吗?如果指针不指向内存地址,是否必须指向 NULL?
我看到 getline(&line, &len, fp) != -1
如果操作正确完成,getline 的 return 值是多少。
#include <stdio.h>
int main(int argc, char * argv[])
{
FILE * fp; // file pointer
char * line = NULL;
int len = 0;
int cnt = 0;
if( argc < 3)
{
printf("Insufficient Arguments!!!\n");
printf("Please use \"program-name file-name N\" format.\n");
return -1;
}
// open file
fp = fopen(argv[1],"r");
// checking for file is exist or not
if( fp == NULL )
{
printf("\n%s file can not be opened !!!\n",argv[1]);
return 1;
}
// read lines from file one by one
while (getline(&line, &len, fp) != -1)
{
cnt++;
if ( cnt > atoi(argv[2]) )
break;
printf("%s",line); fflush(stdout);
}
// close file
fclose(fp);
return 0;
}
参考http://man7.org/linux/man-pages/man3/getline.3.html
ssize_t getline(char **lineptr, size_t *n, FILE *stream);
If *lineptr is set to NULL and *n is set 0 before the call, then
getline() will allocate a buffer for storing the line. This buffer
should be freed by the user program even if getline() failed.
Alternatively, before calling getline(), *lineptr can contain a
pointer to a malloc(3)-allocated buffer *n bytes in size. If the
buffer is not large enough to hold the line, getline() resizes it
with realloc(3), updating *lineptr and *n as necessary.
In either case, on a successful call, *lineptr and *n will be updated
to reflect the buffer address and allocated size respectively.
因此,如果您将 NULL
指针和值为 0 的 len
传递给它,getline
将分配内存。循环内的未来调用不会分配新内存,而是使用相同的地址。 realloc
长度不足时调用
此外,您应该在最后释放 line
作为一个好习惯。即使 getline
已返回 -1
,也应调用此 free
注意这个声明
char * line = NULL;
所以最初指针值是NULL
。在 main 中没有明确重新分配指针。
另一方面看看这个调用
getline(&line, &len, fp)
正如所见,指针线是通过引用传递给函数的。这意味着在函数内可以更改指针并可以将新值分配给指针。
因此,如果最初指针等于 NULL,则可以通过两种方式在函数中重新分配指针。
第一个是当函数有一个局部数组时,该数组具有静态存储持续时间,指针由数组第一个元素的地址分配。但是这种方法并不灵活。
第二种方法是当函数动态重新分配指针指向的内存时。
在最后一种情况下,当不再需要时,您必须释放 main 中指针行指向的已分配内存。
您还可以阅读函数的描述,其中明确指出函数动态分配分配给指针的内存地址。
例如在 Linux 的手册页中写着
If *lineptr is set to NULL and *n is set 0 before the call, then
getline() will allocate a buffer for storing the line. This buffer
should be freed by the user program even if getline() failed.
Alternatively, before calling getline(), *lineptr can contain a
pointer to a malloc(3)-allocated buffer *n bytes in size. If the
buffer is not large enough to hold the line, getline() resizes it
with realloc(3), updating *lineptr and *n as necessary.
In either case, on a successful call, *lineptr and *n will be updated
to reflect the buffer address and allocated size respectively.
这段代码我不明白为什么*line指针没有malloc,getline是动态分配内存的吗?如果指针不指向内存地址,是否必须指向 NULL?
我看到 getline(&line, &len, fp) != -1
如果操作正确完成,getline 的 return 值是多少。
#include <stdio.h>
int main(int argc, char * argv[])
{
FILE * fp; // file pointer
char * line = NULL;
int len = 0;
int cnt = 0;
if( argc < 3)
{
printf("Insufficient Arguments!!!\n");
printf("Please use \"program-name file-name N\" format.\n");
return -1;
}
// open file
fp = fopen(argv[1],"r");
// checking for file is exist or not
if( fp == NULL )
{
printf("\n%s file can not be opened !!!\n",argv[1]);
return 1;
}
// read lines from file one by one
while (getline(&line, &len, fp) != -1)
{
cnt++;
if ( cnt > atoi(argv[2]) )
break;
printf("%s",line); fflush(stdout);
}
// close file
fclose(fp);
return 0;
}
参考http://man7.org/linux/man-pages/man3/getline.3.html
ssize_t getline(char **lineptr, size_t *n, FILE *stream);
If *lineptr is set to NULL and *n is set 0 before the call, then getline() will allocate a buffer for storing the line. This buffer should be freed by the user program even if getline() failed.
Alternatively, before calling getline(), *lineptr can contain a pointer to a malloc(3)-allocated buffer *n bytes in size. If the buffer is not large enough to hold the line, getline() resizes it with realloc(3), updating *lineptr and *n as necessary.
In either case, on a successful call, *lineptr and *n will be updated to reflect the buffer address and allocated size respectively.
因此,如果您将 NULL
指针和值为 0 的 len
传递给它,getline
将分配内存。循环内的未来调用不会分配新内存,而是使用相同的地址。 realloc
长度不足时调用
此外,您应该在最后释放 line
作为一个好习惯。即使 getline
已返回 -1
注意这个声明
char * line = NULL;
所以最初指针值是NULL
。在 main 中没有明确重新分配指针。
另一方面看看这个调用
getline(&line, &len, fp)
正如所见,指针线是通过引用传递给函数的。这意味着在函数内可以更改指针并可以将新值分配给指针。
因此,如果最初指针等于 NULL,则可以通过两种方式在函数中重新分配指针。
第一个是当函数有一个局部数组时,该数组具有静态存储持续时间,指针由数组第一个元素的地址分配。但是这种方法并不灵活。
第二种方法是当函数动态重新分配指针指向的内存时。
在最后一种情况下,当不再需要时,您必须释放 main 中指针行指向的已分配内存。
您还可以阅读函数的描述,其中明确指出函数动态分配分配给指针的内存地址。
例如在 Linux 的手册页中写着
If *lineptr is set to NULL and *n is set 0 before the call, then
getline() will allocate a buffer for storing the line. This buffer
should be freed by the user program even if getline() failed.
Alternatively, before calling getline(), *lineptr can contain a
pointer to a malloc(3)-allocated buffer *n bytes in size. If the
buffer is not large enough to hold the line, getline() resizes it
with realloc(3), updating *lineptr and *n as necessary.
In either case, on a successful call, *lineptr and *n will be updated
to reflect the buffer address and allocated size respectively.