Thread: update head pointer in linked list

  1. #1
    Registered User
    Join Date
    Dec 2010
    Location
    Delhi, India
    Posts
    59

    update head pointer in linked list

    Hi, I have a slight confusion about the function for updating head pointer of a linked list.

    Here is the code:

    Code:
    struct node *deleteFirst(struct node *head)
    {
        if(head != NULL)
        {
           // store the old value of head pointer
           struct node *temp = head;
     
           // Change head pointer to point to next node
           head = head->next;
     
           // delete memory allocated for the previous head node
           free(temp);
        }
     
        return head;
    }
    Here, I want to know, as head is a formal parameter of the function, it will be stored on the stack when the function is called and automatically destroyed when the function returns. So returning this head pointer from the function should result in some sort of error/ incorrect result instead of the head pointer being updated. Correct me where I am wrong. Waiting for the replies.

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    think of 'head' as a value, not a memory location. it may be on the stack or in a register. but in any case, the compiler will preserve the value of 'head' after you update it with head->next, again on the stack or in a register, and return that value properly according to the calling convention of the compiler/processor.

    edit:when you want to know what the compiler will do at the machine instruction level, you need to look at the assembly output. if you are interested in what the C language says will happen, thats a different thing and is specified in the language standard (whichever one your compiler follows)
    Last edited by dmh2000; 12-05-2011 at 11:11 AM.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    The object “head” will be destroyed when the function exits, yes. That's OK, though, because you're not returning the address of “head”. You're returning the value of head: you're making a copy.

    This would be like if I showed you a piece of paper with the number 5 on it, which you copied down onto your own piece of paper. When I destroy my piece of paper, you don't care, because you already copied what was on it.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    52
    The result will still be correct. Yes head will be destroyed when the function returns but it will not destroy (or free) what head points to. Just remember head is just a variable that holds a location (pointer) to a value. When you destroy head you will just loose the location but the value still lives on that location. I don't know if I explained it right its a hard topic. Try searching for Pointers and Memory tutorials for better understanding.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by gaurav_13191 View Post
    Hi, I have a slight confusion about the function for updating head pointer of a linked list.

    Here is the code:

    Code:
    struct node *deleteFirst(struct node *head)
    {
        if(head != NULL)
        {
           // store the old value of head pointer
           struct node *temp = head;
     
           // Change head pointer to point to next node
           head = head->next;
     
           // delete memory allocated for the previous head node
           free(temp);
        }
     
        return head;
    }
    Here, I want to know, as head is a formal parameter of the function, it will be stored on the stack when the function is called and automatically destroyed when the function returns. So returning this head pointer from the function should result in some sort of error/ incorrect result instead of the head pointer being updated. Correct me where I am wrong. Waiting for the replies.
    In your example, temp is destroyed when the function exits... head is not.

  6. #6
    Registered User
    Join Date
    Dec 2010
    Location
    Delhi, India
    Posts
    59
    @commontater You mean that formal parameters are not destroyed when the function returns? Please clarify.

    @others Thanks for your replies. What I understood is that though head will be destroyed it will return the address of the location where data exists. Correct me if I am getting this wrong.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by gaurav_13191 View Post
    @commontater You mean that formal parameters are not destroyed when the function returns? Please clarify.
    The value returned is not destroyed... it is held in a CPU register.

    @others Thanks for your replies. What I understood is that though head will be destroyed it will return the address of the location where data exists. Correct me if I am getting this wrong.
    Nope... you got it right.

  8. #8
    Registered User
    Join Date
    Dec 2010
    Location
    Delhi, India
    Posts
    59
    @commontater Ok.. I got it .. thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked list, Head pointing to second node
    By nkbxwb in forum C Programming
    Replies: 3
    Last Post: 10-01-2011, 06:38 PM
  2. next pointer in linked list
    By c_weed in forum C++ Programming
    Replies: 5
    Last Post: 11-15-2010, 06:42 PM
  3. change the head of linked list question..
    By transgalactic2 in forum C Programming
    Replies: 17
    Last Post: 03-21-2009, 02:31 PM
  4. Cant head insert on doubly linked list
    By aciarlillo in forum C++ Programming
    Replies: 4
    Last Post: 09-18-2005, 11:31 AM
  5. head pointer(linked list)
    By Ausandy in forum C Programming
    Replies: 5
    Last Post: 08-24-2002, 01:25 AM