【问题标题】:insertion doesn't take place for bigger size strings更大尺寸的字符串不会插入
【发布时间】:2014-10-20 15:00:39
【问题描述】:

下面的程序不适用于较大的字符串,但它适用于小字符串。我不知道为什么 sortedInsert 函数没有占用字符串的全长。这个程序中也没有使用字符串长度限制。

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

/* Link list node */

struct node

{

    char* pattern;

    struct node* next;

};

/* function to insert a new_node in a list. Note that this

*   function expects a pointer to head_ref as this can modify the

*     head of the input linked list (similar to push())*/

void sortedInsert(struct node** head_ref, struct node* new_node)

{

    struct node* current;

    /* Special case for the head end */

    if (*head_ref == NULL || (strcmp((*head_ref)->pattern ,new_node->pattern)> 0))

    {

        new_node->next = *head_ref;

        *head_ref = new_node;

    }

    else

    {

        /* Locate the node before the point of insertion */

        current = *head_ref;

        while (current->next!=NULL &&

               strcmp(current->next->pattern, new_node->pattern)< 0)

        {

            current = current->next;

        }

        new_node->next = current->next;

        current->next = new_node;

    }

}

/* BELOW FUNCTIONS ARE JUST UTILITY TO TEST sortedInsert */

/* A utility function to create a new node */

struct node *newNode( char * pattern)

{

    /* allocate node */

    struct node* new_node =

        (struct node*) malloc(sizeof(struct node));

      new_node->pattern = (char *)malloc(sizeof(pattern));

    /* put in the data  */

    strcpy(new_node->pattern , pattern);



    new_node->next =  NULL;

    return new_node;

}

/* Function to print linked list */

void printList(struct node *head)

{





   struct node *temp = head;

    while(temp != NULL)

    { 

         printf("\n%s", temp->pattern);

        temp = temp->next;

    }

}

/* Drier program to test count function*/

int main()

{

    /* Start with the empty list */

    struct node* head = NULL;

    struct node* new_node = newNode("a.b.c.d.e.f.g.h.h.j.k.l.m.n.o");

    sortedInsert(&head, new_node);

    new_node = newNode("a.b.c.de.f.g.h.j.k.l.m.t.y.u.k");

    sortedInsert(&head, new_node);

    new_node = newNode("a.b.c.d.ef.g.h.h.k.j.l.y.u.l.p");

    sortedInsert(&head, new_node);

    printf("\n Created Linked List\n");

    printList(head);



    return 0;

}

这是上述程序的输出,可以看到意外的输出。 输出:

创建的链表

a.b.c.d.e.f.)

a.b.c.d.ef.g.h.h.k.j.l.y.u.l.p

a.b.c.de.f.g)

【问题讨论】:

    标签: c sorting data-structures


    【解决方案1】:

    malloc(sizeof(pattern))

    然后

    strcpy(new_node-&gt;pattern , pattern); 不正确。

    更好的是,使用strlen()获取接收到的字符串长度,然后分配内存,然后执行strcpy()memcpy()复制接收到的字符串。

    【讨论】:

    • 我很惊讶它仍然适用于小尺寸字符串(5-7)个字符长。
    • @user3522354 另外,pattern 是一个指针,所以sizeof(pattern) 可能是 8。如果你所有的字符串都有 7 个字节或更少,你的错误代码将起作用。
    • new_node->pattern = (char *)malloc(sizeof(pattern)); printf("sizeofpattern=%d", sizeof(new_node->pattern));它只给了我4个字节而不是8个。
    【解决方案2】:

    试试下面的代码,它会工作的。您必须传递字符串长度而不是字符串本身

        new_node->pattern = (char *)malloc(strlen(pattern)+1);
    
        /* put in the data  */
    
        memcpy(new_node->pattern , pattern,strlen(pattern)+1);
    

    【讨论】:

      【解决方案3】:

      其他答案正确;但是,似乎有一个专用功能strdup 可以做你想做的事:

      // Incorrect code
      new_node->pattern = (char *)malloc(sizeof(pattern));
      strcpy(new_node->pattern , pattern);
      
      // Correct code, provided by user2083356
      new_node->pattern = (char *)malloc(strlen(pattern)+1);
      memcpy(new_node->pattern , pattern,strlen(pattern)+1);
      
      // Does the same; seems easier to type and understand; also handles errors
      new_node->pattern = strdup(pattern);
      

      【讨论】:

      • 谢谢,但我们不能进行内存检查,以防 malloc 对 strdup 失败。
      猜你喜欢
      • 1970-01-01
      • 2011-08-28
      • 2016-03-24
      • 2013-11-25
      • 2021-11-02
      • 2015-08-30
      • 2017-06-20
      • 2018-04-03
      • 2016-01-15
      相关资源
      最近更新 更多