I am using amd64. Just to make sure I am describing this behavior
correctly, when there is no print statement at the line in question,
the program hangs indefinitely.

Otherwise the merge routine appears to work.

On Sun, Feb 7, 2016 at 2:57 PM, Pavan Maddamsetti
<pavan.maddamse...@gmail.com> wrote:
> amd64
>
> On Sun, Feb 7, 2016 at 2:53 PM, Daniel Dickman <didick...@gmail.com> wrote:
>> i cannot confirm on i386. what platform are you on?
>>
>> On Sun, Feb 7, 2016 at 2:36 PM, Pavan Maddamsetti
>> <pavan.maddamse...@gmail.com> wrote:
>>> /*
>>>  * Here 's the code. Try commenting out the line that says "WTF mate!"
>>>  * On my machine, the loop never returns. Somehow printf makes it work.
>>>  * Compiler bug or not? Please compile and confirm.
>>>  */
>>>
>>> #include <stdio.h>
>>> #include <stdlib.h>
>>>
>>> typedef struct List List;
>>> struct List {
>>>         int data;
>>>         List *next;
>>> };
>>>
>>> List *new(int);
>>> List *add(List *, List *);
>>> List *merge(List *, List *);
>>>
>>> int
>>> main(void)
>>> {
>>>         List *a, *b, *t, *p;
>>>         a = b = t = p = NULL;
>>>
>>>         int i;
>>>         for (i = 0; i < 10; i += 2)
>>>                 a = add(a, new(i));
>>>         for (i = 1; i < 10; i += 2)
>>>                 b = add(b, new(i));
>>>
>>>         printf("Printing a...\n");
>>>         for (t = a; t != NULL; t = t->next)
>>>                 printf("%d ", t->data);
>>>         printf("\n");
>>>
>>>         printf("Printing b...\n");
>>>         for (t = b; t != NULL; t = t->next)
>>>                 printf("%d ", t->data);
>>>         printf("\n");
>>>
>>>         printf("Merging a and b...");
>>>         p = merge(a, b);
>>>         printf("done.\n");
>>>
>>>         printf("Printing...\n");
>>>         for (t = p; t != NULL; t = t->next)
>>>                 printf("%d ", t->data);
>>>         printf("\n");
>>>
>>>         exit(0);
>>> }
>>>
>>> List *
>>> new(int data)
>>> {
>>>         List *newp;
>>>         newp = malloc(sizeof(List));
>>>         newp->data = data;
>>>         newp->next = NULL;
>>>         return newp;
>>> }
>>>
>>> List *
>>> add(List *listp, List *newp)
>>> {
>>>         List *p;
>>>
>>>         if (listp == NULL)
>>>                 return newp;
>>>         for (p = listp; p->next != NULL; p = p->next)
>>>                 ;
>>>         p->next = newp;
>>>         return listp;
>>> }
>>>
>>> List *
>>> merge(List *ap, List *bp)
>>> {
>>>         List *listp;
>>>         List **last;
>>>         last = &listp;
>>>
>>>         for (;;) {
>>>                 if (ap == NULL) {
>>>                         (*last)->next = bp;
>>>                         break;
>>>                 } else if (bp == NULL) {
>>>                         (*last)->next = ap;
>>>                         break;
>>>                 }
>>>                 printf("\nWTF mate!\n"); /* problem here */
>>>                 if (ap->data < bp->data) {
>>>                         (*last)->next = ap;
>>>                         ap = ap->next;
>>>                 } else {
>>>                         (*last)->next = bp;
>>>                         bp = bp->next;
>>>                 }
>>>                 last = &((*last)->next);
>>>         }
>>>         return listp->next;
>>> }

Reply via email to