【问题标题】:having an issue with strtok in cc中的strtok有问题
【发布时间】:2019-02-06 20:47:52
【问题描述】:

我已经编写了一些代码来帮助我在程序中为更大的链表分隔破折号,但是当我调用该类时,我的代码卡在分隔符循环中并且不会死。我什至不能用 kill 命令杀死它 我必须打开一个新的 ssh 客户端

int  deliminator(char word[], struct node *root){
    struct node *start =  (struct node*) malloc(sizeof(struct node));
    struct node *trav = (struct node*) malloc(sizeof(struct node));
    start->next= trav;
    trav->prev = start;

    char *token;
    token = strtok(word,"-");

    while(token){
            /* this loop is broken */

            printf("%s",token);
            struct node *curr  = (struct node*) malloc(sizeof(struct node));
            strcpy(curr->set, token);
            trav->next = curr;
            curr->prev = trav;
            trav = trav->next;

            token = strtok(NULL,"-");
    };
    root->next = start;
    return(0);


};

当我尝试通过循环不正确地运行 strtok 时 令牌 = strtok(令牌,“-”);它卡在第一个令牌上。我似乎找不到问题,我的一个朋友建议它与链表节点有关,但我删除了它们,我遇到了同样的问题。

我在此代码片段中调用分隔符类。

int main(int argc, char *argv[]){
    struct node *root = (struct node*) malloc(sizeof(struct node));
    struct node *trav = (struct node*) malloc(sizeof(struct node));
    root->next = trav;
    if(argc == 2){
    /*only one giant string*/
    deliminator(argv[1],root);
    while(root->next!= NULL){
    printf("%s",root->set);
    };

【问题讨论】:

  • 什么是curr->set?在复制到它之前,你是否为它分配了内存?
  • 请显示您调用deliminator的程序部分,特别是word的类型和初始化;同时显示struct node的定义。
  • 你的循环 while(root->next!= NULL){ printf("%s",root->set); }; 永远不会改变 root — 你需要在循环中的某处相当于 root = root->next; 的东西,担心是否可以改变 root 本身(它可能不是— 你需要它来释放内存)。
  • 此外,您永远不会将 any 节点的 next 指针设置为 null,并且不能安全地依赖 malloc() 为您执行此操作。您需要将next(和prev)指针显式设置为null,这是它们应该具有的值。
  • 您的deliminator 函数创建了两个struct node 对象,而不为它们的set 成员分配任何东西,并将它们插入到列表中,位于您复制令牌的任何节点之前。当您随后在main() 中打印这些节点的set 成员时,您调用了未定义的行为。

标签: c linked-list strtok csv


【解决方案1】:

您的代码大部分结构良好,并且正确使用了strtok。但是您不会初始化变量或分配节点内的字段。我将您的调用从 strcpy 切换到 strdup,以便您分配内存,并使用 calloc 而不是 malloc,以便将指针初始化为 null。在 deliminator 中,只需要分配循环内的节点,只需要保留一个指针 trav 来遍历链表,根节点就可以不用管了。

我让你弄清楚如何不浪费根节点上的内存,你并不真正需要。您应该只有一个根指针,并将其地址传递给分隔符。此外,您应该在退出之前清理并释放 strdup 中的节点和分配的字符串。

int  deliminator(char word[], struct node *root) {
            struct node *trav = root;

            char *token;
            token = strtok(word,"-");

            while(token){
                            /* this loop is fixed! */

                            printf("DEBUG: %s\n",token);
                            struct node *curr  = calloc(1, sizeof(struct node));
                            curr->set = strdup(token);
                            trav->next = curr;
                            curr->prev = trav;
                            trav = trav->next;

                            token = strtok(NULL,"-");
            };
            return(0);


}

int main(int argc, char *argv[]){
            struct node *root = calloc(1, sizeof(struct node));
            if(argc == 2){
                            /*only one giant string*/
                            deliminator(argv[1],root);
                            root = root-> next;
                            while(root != NULL){
                                            printf("%s\n",root->set);
                                            root = root->next;
                            }
            }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-11
    • 1970-01-01
    • 2012-10-16
    • 1970-01-01
    • 1970-01-01
    • 2011-12-04
    相关资源
    最近更新 更多