0

Below programme doesn’t work for bigger size strings , but it works properly for small strings.I am not sure why sortedInsert function is not taking full length of string.There is no string length constraint also being used in this programme.

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

}

Here is the output of above programme, where unexpected output can be seen. Output:

Created Linked List

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)

user3522354
  • 223
  • 1
  • 8

3 Answers3

1

malloc(sizeof(pattern))

and then

strcpy(new_node->pattern , pattern); not correct.

better, use strlen() to get the received string length, then allocate memory, then perform strcpy() or memcpy() to copy the recived string.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • i am surprised how it still works for small size strings(5-7) character long. – user3522354 Oct 20 '14 at 15:28
  • @user3522354 Also, `pattern` is a pointer, so `sizeof(pattern)` is probably 8. If all your strings have 7 bytes or less, your incorrect code will work. – anatolyg Oct 20 '14 at 15:34
  • new_node->pattern = (char *)malloc(sizeof(pattern)); printf("sizeofpattern=%d", sizeof(new_node->pattern));It gives me 4 byte only not 8. – user3522354 Oct 20 '14 at 15:49
0

Try with below code , it will work. you must pass string length instead of string itself

    new_node->pattern = (char *)malloc(strlen(pattern)+1);

    /* put in the data  */

    memcpy(new_node->pattern , pattern,strlen(pattern)+1);
anatolyg
  • 26,506
  • 9
  • 60
  • 134
user2083356
  • 125
  • 1
  • 9
0

Other answers are correct; however, there seems to be a dedicated function strdup to do what you want:

// 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);
Community
  • 1
  • 1
anatolyg
  • 26,506
  • 9
  • 60
  • 134