想在 strtok 之后释放我的指针令牌
我已经提取了代码的“含义”部分(并且还替换了一些行以简化它)。
我有 2 个动态指针,一个用于当前行(从文件中提取),第二个用于当前标记。 在这个问题之后, 在处理完整字符串之前释放/删除 strtok_r 指针? 我写了这个:
int main(void) {
int n = 455;
char *tok2, *freetok2;
char *line, *freeline;
line = freeline = malloc(n*sizeof(*line));
tok2 = freetok2 = malloc(n*sizeof(*tok2));
/* content of the file) */
const char* file_reading = "coucou/gniagnia/puet/";
/* reading from the file */
strcpy(line, file_reading);
strtok(line, "/");
/* get the second token of the line */
tok2 = strtok(NULL, "/");
fprintf(stdout, "%s \n", tok2); // print gniagnia
fprintf(stdout, "%s \n", line); // print coucou
/* free error */
//free(tok2);
/* worked, but maybe don't free "everything ?" */
//free(line);
free(freetok2);
free(freeline);
return 0;
}
但最后,我不确定什么是正确的或不正确的,我发现这个解决方案不是那么优雅(因为使用了2个“保存变量”。
这是正确的吗?有没有一些方法可以改进它? 谢谢
编辑:为此更改了我的代码(它将处理文件的所有行)
include <unistd.h>
include <stdlib.h>
int main(void) {
char *tok2;
char *line;
/* content of the file) */
const char* file_reading = "coucou/gniagnia/puet/";
const char* file_reading2 = "blabla/dadada/";
/* reading from the file */
line = strdup(file_reading);
strtok(line, "/");
/* get the second token of the line */
tok2 = strtok(NULL, "/");
printf("%s \n", tok2);
printf("%s \n", line);
/* reading from the file */
line = strdup(file_reading2);
strtok(line, "/");
/* get the second token of the line */
tok2 = strtok(NULL, "/");
printf("%s \n", tok2);
printf("%s \n", line);
free(line);
return 0;
}
I have extracted the "meaning" part of my code (and also replace some line to simplify it).
I have 2 dynamic pointers, one for the current line (extracted from a file) and a second for the current token.
Following this question, Free/delete strtok_r pointer before processing complete string?
I wrote this :
int main(void) {
int n = 455;
char *tok2, *freetok2;
char *line, *freeline;
line = freeline = malloc(n*sizeof(*line));
tok2 = freetok2 = malloc(n*sizeof(*tok2));
/* content of the file) */
const char* file_reading = "coucou/gniagnia/puet/";
/* reading from the file */
strcpy(line, file_reading);
strtok(line, "/");
/* get the second token of the line */
tok2 = strtok(NULL, "/");
fprintf(stdout, "%s \n", tok2); // print gniagnia
fprintf(stdout, "%s \n", line); // print coucou
/* free error */
//free(tok2);
/* worked, but maybe don't free "everything ?" */
//free(line);
free(freetok2);
free(freeline);
return 0;
}
But at the end, I'm not sure of what is correct or not, and I find this solution not so elegant (because of using 2 "save variables".
Is that correct ? Is there some ways to improve it ?
Thanks
Edit: changed my code for this, (and it will handle all the lines of the file)
include <unistd.h>
include <stdlib.h>
int main(void) {
char *tok2;
char *line;
/* content of the file) */
const char* file_reading = "coucou/gniagnia/puet/";
const char* file_reading2 = "blabla/dadada/";
/* reading from the file */
line = strdup(file_reading);
strtok(line, "/");
/* get the second token of the line */
tok2 = strtok(NULL, "/");
printf("%s \n", tok2);
printf("%s \n", line);
/* reading from the file */
line = strdup(file_reading2);
strtok(line, "/");
/* get the second token of the line */
tok2 = strtok(NULL, "/");
printf("%s \n", tok2);
printf("%s \n", line);
free(line);
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您实际上并没有使用
freetok2
指向的内存,您不需要malloc
任何东西,因此您不需要freetok2
多变的。在代码中使用
free(line)
或free(freeline)
是相同的,因此您不需要freeline 根本就没有。
另一个问题是:
malloc(n*sizeof(*line));
。您可能会说:malloc(n);
因为sizeof(char)
始终为 1。但最重要的是:You're not actually using the memory pointed by
freetok2
, you don't need tomalloc
anything, thus you don't need thefreetok2
variable.Saying
free(line)
orfree(freeline)
is the same in your code so you don't need thefreeline
at all.Another problem is this:
malloc(n*sizeof(*line));
. You might as well be saying:malloc(n);
becausesizeof(char)
is always 1. But best of all would be:代码应修改如下:
The code should be modified as follows: