2

So, I tried doing something like this :

void place(struct node * list, int elem){                                                                                                                                                         
    struct node *tmp = list;                                                                                                                                                                      
    struct node *prev ;                                                                                                                                                                                                                                                                                                                                             
    while(tmp && tmp->info <= elem){                                                                                                                                                              
        prev = tmp;                                                                                                                                                                               
        tmp = tmp->next;                                                                                                                                                                          
    }                                                                                                                                                                                             
    struct node *new = (struct node *)malloc(sizeof(struct node));                                                                                                                                
    new->info = elem;                                                                                                                                                                             
    new->next = prev->next;                                                                                                                                                                       
    prev->next = new;                                                                                                                                                                             
}

And it gave me a segmentation fault. gdb didn't help - showed a backtrace full of 000000 and ??.

But when I tried this :

void place(struct node * list, int elem){                                                                                                                                                         
    struct node *tmp = list;                                                                                                                                                                      
    struct node *prev = tmp;                                                                                                                                                                                                                                                                                                                                             
    while(tmp && tmp->info <= elem){                                                                                                                                                              
        prev = tmp;                                                                                                                                                                               
        tmp = tmp->next;                                                                                                                                                                          
    }                                                                                                                                                                                             
    struct node *new = (struct node *)malloc(sizeof(struct node));                                                                                                                                
    new->info = elem;                                                                                                                                                                             
    new->next = prev->next;                                                                                                                                                                       
    prev->next = new;                                                                                                                                                                             
}

It worked fine ! The only difference between the two is that I'm initializing the local variable pointer prev in the second case, while I'm not doing so in the first case. But I can't see why the first case should be a segmentation fault ?

Can someone please explain this ?

Thanks!

4 Answers 4

6

Consider what happens if the while() condition is false when it's first encountered. prev will never be assigned anything sensible.

Sign up to request clarification or add additional context in comments.

Comments

4

If your while loop fails, prev is never assigned a value and therefore the prev->next value is undefined. Assigning the tmp into prev fixes this.

Comments

1

It is easy. tmp && tmp->info <= elem could be false on the first iteration and new->next = prev->next; would raise a segmentation fault, because prev variable is not initialized

Comments

1

This would not compile in neither C nor C++.

  • If you compiled in C, you wouldn't be able to declare a variable below the while loop.
  • If you compiled in C++, you would get a syntax error for using the "new" keyword.

Turns out you are using a non-standard compiler. You should get one that conforms to either the ISO C or the ISO C++ standard!

1 Comment

Nice - I'm using gcc 4.4.3 - maybe I've to enable C99 syntax checking in it for it to conform to what you're saying, it seems to be a little lax right now :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.