每次调用strtok_r()(从字符串中提取标记)后,我不应该释放s_ptr吗?
static void get_uevent_info(struct media_device_entry *md_ptr, char *dname)
{
FILE *fd;
char file[PATH_MAX], *name, *p;
char s[1024];
char *s_ptr;
snprintf(file, PATH_MAX, "%s/%s/uevent", dname, md_ptr->node);
fd = fopen(file, "r");
if (!fd)
return;
while (fgets(s, sizeof(s), fd)) {
p = strtok_r(s, "=", &s_ptr);
if (!p)
continue;
name = p;
p = strtok_r(NULL, "\n", &s_ptr);
if (!p)
continue;
if (!strcmp(name, "MAJOR"))
md_ptr->major = atol(p);
else if (!strcmp(name, "MINOR"))
md_ptr->minor = atol(p);
}
fclose(fd);
}
Run Code Online (Sandbox Code Playgroud)
我从未使用过这个功能,所以也许我错了.
最好的祝福.
s_ptr不应该被释放,因为它没有被mallocor calloc或者分配realloc.手册页说,
The saveptr argument is a pointer to a char * variable that is used internally by strtok_r() in order to maintain context between successive calls that parse the same string.
也就是说,strtok_r不分配内存.所以只需将指针地址发送到char即.char **就足够了,返回后无需任何操作.即使char *没有初始化也没关系,但仍然初始化指针NULL是避免恶梦的好习惯.
| 归档时间: |
|
| 查看次数: |
1146 次 |
| 最近记录: |