小编gui*_*ouz的帖子

strtok和内存泄漏

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

#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 …
Run Code Online (Sandbox Code Playgroud)

c memory malloc free strtok

5
推荐指数
2
解决办法
2万
查看次数

在ac函数中分配一个数组

我正在尝试在函数内部分配和初始化一个数组,但我似乎无法在返回后获取值.

这是我最近几乎在努力的尝试

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

int func(int **thing);

int main() {

 int *thing;

 func(&thing);

 printf("%d %d", thing[0], thing[1]);
}

int func(int **thing) {

 *thing = calloc(2, sizeof(int));

 *thing[0] = 1;
 *thing[1] = 2; 

 printf("func - %d %d \n", *thing[0], *thing[1]);
}
Run Code Online (Sandbox Code Playgroud)

但是在函数外部打印的值是1和0.有很多关于指针的文档,但我没有发现这个特定的案例.关于我做错的任何提示?

c pointers

3
推荐指数
2
解决办法
2356
查看次数

标签 统计

c ×2

free ×1

malloc ×1

memory ×1

pointers ×1

strtok ×1