想在 strtok 之后释放我的指针令牌

发布于 2025-01-04 08:55:40 字数 1855 浏览 1 评论 0原文

我已经提取了代码的“含义”部分(并且还替换了一些行以简化它)。

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雾里花 2025-01-11 08:55:40

您实际上并没有使用 freetok2 指向的内存,您不需要 malloc 任何东西,因此您不需要 freetok2多变的。

在代码中使用 free(line)free(freeline) 是相同的,因此您不需要 freeline 根本就没有。

另一个问题是:malloc(n*sizeof(*line));。您可能会说: malloc(n); 因为 sizeof(char) 始终为 1。但最重要的是:

line = malloc(strlen(file_reading) + 1);
strcpy(line, file_reading);

You're not actually using the memory pointed by freetok2, you don't need to malloc anything, thus you don't need the freetok2 variable.

Saying free(line) or free(freeline) is the same in your code so you don't need the freeline at all.

Another problem is this: malloc(n*sizeof(*line));. You might as well be saying: malloc(n); because sizeof(char) is always 1. But best of all would be:

line = malloc(strlen(file_reading) + 1);
strcpy(line, file_reading);
完美的未来在梦里 2025-01-11 08:55:40

代码应修改如下:

int main(void) {
    int n = 455;  
    char *tok2;
    char *line;

    line = malloc(n*sizeof(*line));

    /* 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(line);
    return 0;
}

The code should be modified as follows:

int main(void) {
    int n = 455;  
    char *tok2;
    char *line;

    line = malloc(n*sizeof(*line));

    /* 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(line);
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文