GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 31-Aug-2009, 11:44
aijazbaig1's Avatar
aijazbaig1 aijazbaig1 is offline
Member
 
Join Date: May 2006
Location: India
Posts: 156
aijazbaig1 has a spectacular aura aboutaijazbaig1 has a spectacular aura about
Question

NULL pointer assignment results in segmentation fault


Hello,

I am trying to write a snippet which deletes a given node in a linked list. The snippet reads:
CPP / C++ / C Code:
typedef struct node {
    int num;
    struct node *next;
} Node;

int delnode(Node **p_strtlst,int elmt)
{
    Node *tmp = *p_strtlst;
    Node *tmp_start = NULL;

    int flag = 0;    

    while(tmp != NULL) {
        if(tmp -> num == elmt) { //element found
            if(tmp_start == NULL) { //remove 1st element
                printf("rem 1st elmt (*p_strtlst) -> num = %d\n",(*p_strtlst) -> num);                
                *p_strtlst = (*p_strtlst) -> next; //--> apparently fails when RHS is NULL
                printf("*p_strtlst now points at %d, and *tmp points at %d\n",(*p_strtlst) -> num,tmp -> num);
                printf("flag = %d\n",flag);
            }
            else{
                printf("tmp -> num = %d\n",tmp -> num);
                tmp_start -> next = tmp -> next;
                printf("flag = %d\n",flag);
            }
            free(tmp);
            puts("after freeing list is:");
            printlist(*p_strtlst);
            flag = 1;
            break;
        }
        else { //element not found move on to the next
            tmp_start = tmp;
            tmp = tmp -> next;
        }
    }
    printf("\nflag = %d\n",flag);
    return flag;
}

I wonder why the line specified above fails when the RHS is a NULL. That is it looks like I cannot have '*p_strtlst ' point at NULL.
__________________
Hope to hear from you guys!

--------------------------------------------------

Best Regards,
Aijaz Baig.
  #2  
Old 31-Aug-2009, 13:19
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: NULL pointer assignment results in segmentation fault


Quote:
Originally Posted by aijazbaig1
...
I wonder why the line specified above fails when the RHS is a NULL. That is it looks like I cannot have '*p_strtlst ' point at NULL.

Well, you certainly are allowed to have a pointer value equal to NULL.

But... you are never allowed to dereference a NULL pointer.

If your list has exactly one element and you delete it with your code, notice that after deleting the node, *p_strtlst is NULL. You then try to print
out (*p_strtlst)->num



Try the following to see exactly how it crashes:
CPP / C++ / C Code:
        printf("rem 1st elmt (*p_strtlst) -> num = %d\n",(*p_strtlst) -> num);

        printf("(*p_strtlst)->next = %p\n", (void *)(*p_strtlst)->next);

        *p_strtlst = ((*p_strtlst) -> next); 

        printf("So far, so good, (*p_strtlst) = %p, tmp = %p\n", (void *)(*p_strtlst), (void *)tmp);
  
        /* The following crashes if the list only has one node */
        printf("*p_strtlst now points at %d, and *tmp points at %d\n",(*p_strtlst) -> num,tmp -> num);

Then change the print statements so that you never dereference the NULL pointer of an empty list. Maybe something like:

CPP / C++ / C Code:
        printf("rem 1st elmt (*p_strtlst) -> num = %d\n",(*p_strtlst) -> num);

        printf("(*p_strtlst)->next = %p\n", (void *)(*p_strtlst)->next);

        *p_strtlst = ((*p_strtlst) -> next); /*--> apparently fails when RHS is NULL */

        if (*p_strtlst) {
            printf("(*p_strtlst)->num = %d, and ", (*p_strtlst)->num);
        }

        printf("*tmp->num =  %d\n",tmp->num);


Regards,

Dave
Last edited by davekw7x : 31-Aug-2009 at 14:13.
 
 

Recent GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
20 questions game monkeeass83 C++ Forum 1 21-Mar-2008 03:32
Binary Search Tree Peter_APIIT C Programming Language 13 19-Mar-2008 23:11
Str_Misaligned in Double Link List Peter_APIIT C Programming Language 1 29-Feb-2008 20:50
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
[Tutorial] Pointers in C (Part II) Stack Overflow C Programming Language 0 27-Apr-2005 17:36

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:38.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.