CurrentElement->next = CurrentElement->next->next (UNDEFINED?)

Discussion in 'C Programming' started by Deniz Bahar, Mar 8, 2005.

  1. Deniz Bahar

    Deniz Bahar Guest

    Hi,

    I'm working with a single linked list and want to delete elements by
    searching through the list (starting form the HEAD) then finding the
    element, then doing the following:

    NewElement = CurrentElement->next;
    CurrentElement->next = NewElement->next->next;
    free(NewElement);

    Does the double ->next invoke undefined behaviour (sequence points
    etc)?



    ========
    The full function follows:
    ========

    typedef struct ElementTag Element;

    extern Element *NewElement;
    extern Element *CurrentElement;
    extern Element *HeadElement;

    /* Finds and deletes person- returns 1, returns 0 for failure) */
    int
    FindDeletePerson(const char *name)
    {
    if(!strcmp(HeadElement->Person.name, name))
    {
    NewElement = HeadElement->next;
    free(HeadElement);
    HeadElement = NewElement;
    return 1;
    }
    else
    {
    CurrentElement = HeadElement;
    while(CurrentElement->next)
    {
    if(!strcmp(CurrentElement->next->Person.name, name))
    {
    NewElement = CurrentElement->next;
    CurrentElement->next = NewElement->next->next;
    free(NewElement);
    return 1;
    }
    CurrentElement = CurrentElement->next;
    }
    }
    return 0;
    }
    Deniz Bahar, Mar 8, 2005
    #1
    1. Advertising

  2. Deniz Bahar

    Quentarez Guest

    On 8 Mar 2005 14:26:21 -0800, Deniz Bahar wrote:

    > Hi,
    >
    > I'm working with a single linked list and want to delete elements by
    > searching through the list (starting form the HEAD) then finding the
    > element, then doing the following:
    >
    > NewElement = CurrentElement->next;
    > CurrentElement->next = NewElement->next->next;
    > free(NewElement);
    >
    > Does the double ->next invoke undefined behaviour (sequence points
    > etc)?
    >


    <snip>

    You can use as many ->next as you wish, as long as the ->next link exists.
    Quentarez, Mar 8, 2005
    #2
    1. Advertising

  3. Deniz Bahar wrote:
    > ...
    > I'm working with a single linked list and want to delete elements by
    > searching through the list (starting form the HEAD) then finding the
    > element, then doing the following:
    >
    > NewElement = CurrentElement->next;
    > CurrentElement->next = NewElement->next->next;
    > free(NewElement);
    >
    > Does the double ->next invoke undefined behaviour (sequence points
    > etc)?
    > ...


    No. Firstly, you are not accessing the same object twice.
    'CurrentElement->next' as an lvalue is neither 'NewElement' nor
    'NewElement->next' nor 'NewElement->next->next'. There's no problem with
    sequence points here.

    However, the code itself doesn't do what it is supposed to do. Doing

    NewElement = CurrentElement->next;
    CurrentElement->next = NewElement->next->next;

    will exclude _two_ consecutive elements from the list (both 'NewElement'
    and 'NewElement->next'), not one. Apparently, this is not what you
    wanted to do. You probably intended to do either

    NewElement = CurrentElement->next;
    CurrentElement->next = NewElement->next;

    or

    NewElement = CurrentElement->next;
    CurrentElement->next = CurrentElement->next->next;

    That would exclude only one element from the list.

    Now in that last version the value of 'CurrentElement->next' is accessed
    twice (once to read it and once to modify it), which might rise the
    question about sequence points etc. However, it this case everything is
    fine too. The old value of 'CurrentElement->next' is read for the sole
    purpose of determining its new value.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Mar 9, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mantorok Redgormor
    Replies:
    70
    Views:
    1,734
    Dan Pop
    Feb 17, 2004
  2. =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki

    Reading of file by next of map file and by next of file descriptor.

    =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki, Jul 10, 2007, in forum: C++
    Replies:
    1
    Views:
    780
    Alf P. Steinbach
    Jul 10, 2007
  3. VK
    Replies:
    45
    Views:
    582
    Dr John Stockton
    Sep 12, 2006
  4. Replies:
    20
    Views:
    288
  5. -Lost
    Replies:
    13
    Views:
    359
    Richard Cornford
    Jan 31, 2007
Loading...

Share This Page