【问题标题】:Want to free my pointer token after strtok想在 strtok 之后释放我的指针令牌
【发布时间】:2012-02-10 15:27:19
【问题描述】:

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

我有 2 个动态指针,一个用于当前行(从文件中提取),另一个用于当前标记。 在这个问题之后,Free/delete strtok_r pointer before processing complete string? 我写了这个:

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;
}

【问题讨论】:

    标签: c char free strtok


    【解决方案1】:

    你实际上并没有使用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);
    

    【讨论】:

    • 你说我不需要 freetok2 变量,但如果我尝试 free(tok2);我会得到一个堆错误(空闲的无效指针)。否则,我没有为ntc加上+1,因为我已经使用了一个int(事先不知道我的字符串的大小,我只知道它会小于300 char)。
    • @roro 你不需要free(tok2)。你只需要free(line)
    • 我期待这样的事情,但这怎么可能?我想遵循强大的“规则”释放你分配的一切..
    • @roro line = freeline = malloc() 仅分配一个,而不是两个,因此您只需释放其中一个。此外,您无需为 line 分配 455 个字符即可为其分配 file_reading。如果 file_reading 超过 455 个字符怎么办? strdup(file_reading) 更好,它会自动分配足够的内存。
    • @roro 释放你分配的所有东西但你没有分配tok2,是吗?
    【解决方案2】:

    代码修改如下:

    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;
    }
    

    【讨论】:

    • 好的,我根本不需要分配tok2。我觉得这很奇怪,因为这听起来像是使用 char* 而不分配它。
    猜你喜欢
    • 2017-01-09
    • 1970-01-01
    • 2014-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-28
    • 1970-01-01
    相关资源
    最近更新 更多