strtok和内存泄漏 [英] strtok and memory leaks

查看:565
本文介绍了strtok和内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用strtok()编写了一个简单的URL解析器.这是代码

I wrote a simple url parser using strtok(). here's the code

#include <stdio.h>
#include <stdlib.h>

typedef struct {
    char *protocol;
    char *host;
    int port;
    char *path;
} aUrl;


void parse_url(char *url, aUrl *ret) {

    printf("Parsing %s\n", url);
    char *tmp = (char *)_strdup(url);
    //char *protocol, *host, *port, *path;
    int len = 0;

    // protocol agora eh por exemplo http: ou https:
    ret->protocol = (char *) strtok(tmp, "/");
    len = strlen(ret->protocol) + 2;

    ret->host = (char *) strtok(NULL, "/");


    len += strlen(ret->host);

    //printf("char at %d => %c", len, url[len]);

    ret->path = (char *)_strdup(&url[len]);

    ret->path = (char *) strtok(ret->path, "#");

    ret->protocol = (char *) strtok(ret->protocol, ":");

    // host agora é por exemplo address.com:8080
    //tmp = (char *)_strdup(host);
    //strtok(tmp, ":");
    ret->host = (char *) strtok(ret->host, ":");
    tmp = (char *) strtok(NULL, ":");

    if(tmp == NULL) {
        if(strcmp(ret->protocol, "http") == 0) {
            ret->port = 80;
        } else if(strcmp(ret->protocol, "https") == 0) {
            ret->port = 443;
        }
    } else {
        ret->port = atoi(tmp);
    }


    //host = (char *) strtok(NULL, "/");




}

/*
 * 
 */
int main(int argc, char** argv) {
    printf("hello moto\n");

    aUrl myUrl;
    parse_url("http://teste.com/Teste/asdf#coisa", &myUrl);


    printf("protocol is %s\nhost is %s\nport is %d\npath is %s\n", myUrl.protocol, myUrl.host, myUrl.port, myUrl.path);

    return (EXIT_SUCCESS);
}

如您所见,我经常使用strtok()以便对URL进行切片".我不需要支持不同于http或https的URL,因此它的完成方式解决了我所有的问题. 我担心的是(这是在嵌入式设备上运行的)-我是否在浪费内存? 当我写类似

As you can see, I use strtok() a lot so I can "slice" the url. I don't need to support urls different than http or https so the way it's done solves all of my problems. My concern is (this is running on an embedded device) - Am I wasting memory ? When I write something like

ret->protocol = (char *) strtok(tmp, "/");

然后致电

ret->protocol = (char *) strtok(ret->protocol, ":");

我是否保留的第一个指针ret-> protocol保留在内存中?我认为也许应该将第一个调用设置为tmp指针,然后将strtok指向ret-> protocol指向字符串的右侧部分(第二个调用),然后再设置free(tmp).

Does me first pointer ret->protocol held remain in memory ? I thought that maybe I should set the first call to a tmp pointer, call strtok pointing ret->protocol to the right portion of the string (the second call) and then free(tmp).

使用strtok的最佳方法应该是什么?

What should be the best way to use strtok ?

推荐答案

要直接回答您的问题,strtok只返回一个指针,该指针指向您作为输入提供的字符串内的某个位置-它不会为您分配新的内存,因此不需要返回给您返回的任何指针上的free.

To answer your question directly, strtok only returns a pointer to a location inside the string you give it as input-- it doesn't allocate new memory for you, so shouldn't need to call free on any of the pointers it gives you back in return.

对于它的价值,您还可以研究"strchr"和"strstr",它们是在字符串中搜索单个字符或序列的非破坏性方式.

For what it's worth, you could also look into "strchr" and "strstr", which are nondestructive ways of searching for single characters or sequences within strings.

还要注意,这里的内存分配是有问题的-您正在使用strdup()在parse函数中分配一个新字符串,然后将该内存块的片段分配给"ret"字段.因此,您的调用方将负责释放strdup'd字符串,但是由于您仅隐式地将该字符串传递回ret内部,因此调用方需要神奇地知道要传递给free的指针. (可能是ret-> protocol,但可能不是,取决于输入的外观.)

Also note that your memory allocation is problematic here-- you're using strdup() to allocate a new string inside your parse function, and then you're assigning fragments of that memory block to fields of "ret". Your caller will thus be responsible for free'ing the strdup'd string, but since you're only passing that string back implicitly inside ret, the caller needs to know magically what pointer to pass to free. (Probably ret->protocol, but maybe not, depending on how the input looks.)

这篇关于strtok和内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆