0

I am trying to merge two already sorted linked lists temp1 and temp2. I am getting segmentation fault for this example :

Input

temp1 : 1->NULL

temp2 : 2->3->4->5->6->NULL

Output

1st Output

But if i remove "\n" from printf statements then it directly prints segmentation fault.

2nd Output

Why this is happening?

Code :

list *merge(list * temp1, list * temp2)
{
    list *x, *y, *z;

    x = temp1;
    y = temp2;
    print(x);
    print(y);

    z = (list *) malloc(sizeof(list));

    if (x->key < y->key)
    {
        z->key = temp1->key;
        x = x->next;
    }
    else
    {
        z->key = temp2->key;
        y = y->next;
    }
    z->next = NULL;

    while (x || y)
    {
        printf("asdf\n");
        if (x == NULL)
        {
            z = append(z, y->key);
            y = y->next;
            printf("x\n");
        }
        if (y == NULL)
        {
            z = append(z, x->key);
            x = x->next;
            printf("y\n");
        }
        if (x && y)
        {
            printf("x && y\n");
            if (x->key < y->key)
            {
                z = append(z, x->key);
                x = x->next;
            }
            else
            {
                z = append(z, y->key);
                y = y->next;
            }
        }
    }
    print(z);
    return z;
}

Thanks in advance.

I have already removed the segmentation fault. I am asking why it gets delayed when i remove the '\n' from printf statements?

Community
  • 1
  • 1
a0n1i2k3
  • 33
  • 6
  • 1
    Welcome to Stack Overflow! It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Paul R May 06 '16 at 16:19
  • 1
    Are you asking why you're getting a segfault or why you don't see the output when you remove the newlines? – Kevin May 06 '16 at 16:21
  • 1
    Have you tried using a debugger? You're either not allocating a pointer properly, or overwriting undesired memory somewhere, maybe even inside `append`, which isn't showing. You should narrow this down rather than expect others here to fish through all your code to find it. Also, [don't cast the return of `malloc`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – lurker May 06 '16 at 16:21
  • Why i don't see the output when i remove the newlines? – a0n1i2k3 May 06 '16 at 16:25
  • Duplicate of [Execution of printf() and Segmentation Fault](https://stackoverflow.com/q/9469790) – Peter Cordes Dec 31 '21 at 07:44

3 Answers3

4

The reason that you get segmentation fault before you see any output from printf is that usually printf to stdout get flushed if you do fflush(stdout) or if you add \n to get an end of-line. If you get a segmentation fault before stdout got flushed anything in stdout waiting to get flushed will be lost.

Henrik Carlqvist
  • 1,138
  • 5
  • 6
3

The answer by Henrik is straight to the point of the question.

Yet there were a few comments to the OP that suggested using the debugger. Here is what you would get if you went on to step through the code.

Consider the following code at the beginning of the body of the while loop.

if(x == NULL){
    z = append(z,y->key);
    y = y->next;
    printf("x\n");
}
if(y == NULL){
    z = append(z,x->key);
    x = x->next;
    printf("y\n");
}

Let's step through it just before the segfault occurs, that is when

x: NULL
y: 6-> NULL

The x == NULL condition is true, therefore we fall through and execute

y = y-> next

getting

x: NULL
y: NULL

Now the y == NULL condition is also true, therefore we have to execute

z = append(z,x->key)

dereferencing the NULL pointer x. Voila, it's a segfault.

It is not difficult to correct this code.

Dima Chubarov
  • 16,199
  • 6
  • 40
  • 76
  • Actually I have asked question to know why seg fault is getting delayed when i removed '\n' . But Thank You for your answer – a0n1i2k3 May 06 '16 at 16:38
2

Henrik explained very well the discrepancies between the display and the segfault.

The real problem

However, I suppose that you're interested in the root cause of your segmentation fault. In your while (x||y) loop, you have foreseen 3 distinct cases, which seem to be mutually exclusive:

  • if (x==NULL)
  • if (y==NULL)
  • if (x && y)

In fact all this is very logic and it should work perfectly. However, you've forgotten that it the body of each if you modify x or y.

So when you've reached the end of the first list (i.e. x==NULL), you'll go to the next element of the second list, but if you've just reached the end of the second list, you'll execute if(y==NULL) and move PAST THE END OF THE FIRST LIST !! That's the cause of your segmentation fault; you dereference a NULL pointer.

The simple solution

Just use else:

if (x==NULL) 
    ...
else if (y==NULL)
    ...
else if (x && y) 
    ...
Christophe
  • 68,716
  • 7
  • 72
  • 138